Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

overloading constructors

  Asked By: Ketan    Date: May 12    Category: Java    Views: 826
  

I'm currently taking a course in Java in college. I want to know if
anyone can help me out how to overload a constructor without
declaring any private variables. I know that the method name is the
same as the class.

Here is the code:

public class john{
public static void main(String args[]){
int num1=4;
int num2=4;

System.out.println("The numbers are "+num1+" and "+num2);
jmav frisky=new jmav(3,3);
frisky.send(num1,num2);}}


public class jmav{
jmav(int n1,int n2){
num1=n1;
num2=n2;}
public static void send(int n1,int n2){
System.out.println("The numbers are "+n1+" and "+n2);}}


Something was not right when the compiler generated errors when
javacing john.java. Can anyone solve that problem for me? What could
be the correct code for overloading a constructor and send it to
another class?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Calvin Banks     Answered On: May 12

The problem  with ur code  is, u need to declare
variables num1, num2 before the constructor  in class
jmav.java. Also, I dont see any overloading  done here.
If u want try overloading, u can define one more
constructor with 1 parameter or 3 parameter or in 2
parameter constuctor itsef with another type of value
instead of integer ..

 
Answer #2    Answered By: Ralph Murray     Answered On: May 12

actually compiled fine for me once I got jmav compiled. Just
a warning about not using frisky.send statically.

jmav, on the other hand...why are you passing anything to the
constructor? neither john nor jmav have any members. In your
constructor, you are trying to assign values to the two ints privately
declared in john's main. jmav has no access to these vars and it
doesn't need it.

Anyways, classes can have multiple constructors. Java will use the
appropriate one based on what you pass in.

code:

public class  john {
// This code  not changed
}

public class jmav{
jmav() {}
jmav(int n1, int  n2) {}
public static  void send(int n1, int n2) {
System.out.println("The numbers  are " + n1 + " and " + n2);
}
}

 
Answer #3    Answered By: Keith Marshall     Answered On: May 12

can any 1 tell as how will i find/trace the .jsp file that is calling the
.jasper file
to create a pdf display using openreport tools.

 
Didn't find what you were looking for? Find more on overloading constructors Or get search suggestion and latest updates.