|
-
Dec 27th, 2001, 08:46 PM
#1
Thread Starter
Fanatic Member
Problem with constructor overloading
I have this code
class sobre {
sobre (int x)
{
System.out.println(x);
}
sobre (int x, int y)
{
sobre(x);
}
public static void main(String arguments[])
{
sobre s=new sobre(3,21);
}
}
Wich is overloading the constructor... but it tells me C:\Program Files\Xinox Software\JCreator Pro\MyProjects\aaa\sobre.java:12: cannot resolve symbol
symbol : method sobre (int)
location: class sobre
sobre(x);

WHY!!! It should work!!!
"The difference between mad and genius is the success"
-
Dec 27th, 2001, 09:08 PM
#2
try this...
Code:
class sobre {
sobre (int x)
{
System.out.println(x);
}
sobre (int x, int y)
{
this(x);
}
public static void main(String arguments[])
{
sobre s=new sobre(3,21);
}
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 27th, 2001, 09:45 PM
#3
Thread Starter
Fanatic Member
you are the man....
good lord man... you know vb and java... are you an oracle or what?!?!?!
"The difference between mad and genius is the success"
-
Dec 27th, 2001, 09:46 PM
#4
Muwahahahahaha!!!
I am master of all!!!

Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 28th, 2001, 02:34 AM
#5
Dazed Member
Andreex somthing things you might want to
take note of. The this() construct when
used in this manner is called a "constructor call
statement" Using this() in this manner enables
local chaining of constructors. Java places restrictions
on how it is to be used. For instance this can only be
used in a constructor definition. Also this() must appear
as the first statment in a constructor.
Using super() allows your subclass to
influence the intialization of it's inherited state when
an object of the subclass is created. Some of the same
restrictions apply. super() can only appear as the
first statement in a constructor. So basically this()
and super()cannot occur in the same constructor.
Code:
class Light {
private int watts;
private boolean indicator;
Light(){
this(300,false);
System.out.println("Returning from non-default constructor 1 in class Light");
}
Light(int watts, boolean indicator){
super(); // implicitly placed in if not supplied
this.watts = watts;
this.indicator = indicator;
System.out.println("Returning from non-default constructor 2 in class Light");
}
}
class HighPressureSodiumLamp extends Light{
private int heatDisplacement;
HighPressureSodiumLamp(){
this(600,300,false);
System.out.println("Returning from non-default constructor 1 in class HighPressureSodiumLamp");
}
HighPressureSodiumLamp(int heatDisplacment,int watts, boolean indicator){
super(300,false);
this.heatDisplacement = heatDisplacment;
System.out.println("Returning from non-default constructor 2 in class HighPressureSodiumLamp");
}
}
class Test{
public static void main(String[] args){
HighPressureSodiumLamp hps = new HighPressureSodiumLamp();
}
}
Last edited by Dilenger4; Dec 28th, 2001 at 02:50 AM.
-
Dec 28th, 2001, 05:50 PM
#6
Thread Starter
Fanatic Member
So... the code up here.... wouldnt work????
That's very intereseting,,,, thanks for the note!
"The difference between mad and genius is the success"
-
Dec 28th, 2001, 05:57 PM
#7
Thread Starter
Fanatic Member
Humm..... I did something similar and it did work....
"The difference between mad and genius is the success"
-
Dec 28th, 2001, 06:14 PM
#8
Dazed Member
The code that you posted with the change made by crypt would work. My code should compile also.
-
Feb 4th, 2002, 09:55 PM
#9
Thread Starter
Fanatic Member
?????? what was that?!!?
"The difference between mad and genius is the success"
-
Feb 4th, 2002, 10:00 PM
#10
Thread Starter
Fanatic Member
?????? THAT IS WIERD!!!
A MESSAGE IN MY MSN MESSEANGER CAME UP I HAD A NEW EMAIL FORM VBWORLD AND I CAME IN THRU THE LINK... IT BROUGHT ME TO THIS POST... AND YOU (DILIGENT) POSTED A PIC OF A MAGIC LAMP... BUT.. NOW I CAME DIRECTLY TO VBFORUMS AND THAT POST (YOURS) IS GONE... HOW CAN YOU DO THAT?!
AM I ON DRUGS OR WHAT?!?!?!
"The difference between mad and genius is the success"
-
Feb 4th, 2002, 10:00 PM
#11
Dazed Member
-
Feb 7th, 2002, 10:14 PM
#12
Dazed Member
It's magic!
Last edited by Dilenger4; Feb 7th, 2002 at 10:20 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|