• Post Reply Bookmark Topic Watch Topic
  • New Topic

Super as the first line in a method  RSS feed

 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following piece of code:



However, I get the error message that "super" should be the first statement in the construction body. But isn't it that already? (If I remove the "int", this error message disappears, but I would like the method to return a value.)

What am I doing wrong?
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a default constructor here as you didn't define yours.
What kind of return data type constructor has?
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me where?
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java added you one as you didn't define yourself. You didn't answer to my question.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor can't have return type. If you add return type it is no longer a constructor but rather an ordinary method. This is exactly what you have here. So compiler complains about that that you're calling super not from the constructor.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks but how do I get a value from a static method?
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evert DeBoer wrote:OK, thanks but how do I get a value from a static method?

Hi Evert,
Are you familiar with constructors, methods, static methods, this and super ?
If not, I suggest you look at : https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additional links for this and super:
https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
https://docs.oracle.com/javase/tutorial/java/IandI/super.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!