Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

newbie's question about constructor

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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... ...
 
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 74776
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic