aspose file tools
The moose likes Beginning Java and the fly likes newbie's question about constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "newbie Watch "newbie New topic
Author

newbie's question about constructor

SkyGriller tian
Greenhorn

Joined: Jan 27, 2012
Posts: 8



above is what i get from oracle official site. so i tried some exercise myself. below is my code.





the problem is : when i replace "this.day=varia" with "this(varia)", the system just gave me an error about constructor.

could anyone explain why.
tons of thanks!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3141
1. It helps if you copy/paste the exact, complete error message (though I think I know what it is in this case).

2. It also helps if you carefully read the error message, especially the first couple lines, as it usually tells you exactly what you're doing wrong.

3. Look closely and see if you can spot the key structural difference between what you're doing and what the example is doing.


4. If you're doing it for an academic exercise, that's fine, but note that Java has enums at the language level, so you don't need to simulate one.
SkyGriller tian
Greenhorn

Joined: Jan 27, 2012
Posts: 8
sorry,i forgot to say that i had a Day.java file in the same package of EnumDemo.java, the content of which is below

public enum Day{
MONDAY, TUESDAY, WEDNESDAY, THIRSDAY, FRIDAY, SATURDAY, SUNDAY
}


the error message is not in english, i don't know how to translate it exactly into english....

[javadoc]
//myversion
public class EnumDemo {
private Day day;
public EnumDemo(Day varia){
this(varia);
}[/javadoc]

[javadoc]
//tutorial's version
public Rectangle(int width, int height) {
this(0, 0, width, height);
}[/javadoc]


as you can see above, the two versions are quite similar... ...
Chetan Sarnad
Greenhorn

Joined: Nov 26, 2011
Posts: 20
SkyGriller tian wrote:


the problem is : when i replace "this.day=varia" with "this(varia)", the system just gave me an error about constructor.

could anyone explain why.
tons of thanks!


Can you specify your requirement for using this(varia)?
Wont it be a recursive invocation of the constructor? i.e the constructor calling itself.

And if you want to assign values to your instance variables, the correct syntax would be

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3141
SkyGriller tian wrote:
as you can see above, the two versions are quite similar... ...


I already saw how they are similar and how they are different. The difference is very important. It's why you're getting the error. Look closely at my post. It shows the difference.

Even if you cannot translate the error message into English, can you read it and understand it? Between that and my sample code above, you should have an idea of the problem. You can also step through the code yourself, manually, and figure out what each step will do.

Using my example (which is equivalent to your code):


What do you think will happen if we try to execute that code? In particular, what do you think the commented line will do?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
SkyGriller tian wrote: . . . i forgot to say that i had a Day.java file in the same package of EnumDemo.java, . . .
You really ought to tell us all the details. How can we help if we don’t know that sort of information.


the error message is not in english, i don't know how to translate it exactly into english....
Even that information would have been helpful earlier; it would have shown you are making an effort, and somebody else could have tried your code (had it been complete) to get the English error message.







as you can see above, the two versions are quite similar... ...
No, they are not similar. They are both incomplete, so we cannot see the differences properly. Please quote the tutorial you found that on, so we can inspect the whole code. The use of this(...); entails constructor overloading. Have you got an overloaded constructor?
SkyGriller tian
Greenhorn

Joined: Jan 27, 2012
Posts: 8
Chetan Sarnad

your post just exactly presented the error i got.thanks.

and Jeff, thanks, i'm will examine my code again ang again closely! thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3141
SkyGriller tian wrote:
and Jeff, thanks, i'm will examine my code again ang again closely! thanks


You're welcome. I was making the same point Chetan was, but I was trying to get you to see for yourself what the error means and why you were getting it.

Do go back and look at my example and the other example and make sure you understand the difference between what they did and what you were doing in the erroneous code.
 
 
subject: newbie's question about constructor
 
Threads others viewed
Problem when repainting rectangles
Using this() keyword in constructors
Swing graphics problems
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture