• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method  RSS feed

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. What diffrence would happen If I add  ' this. ' to 25,26,27,28 line?

2. Also I need write a method, who would return values from constructor to 'running class' and print them to console . How to do it?






 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You'd have five more characters on each line . Seriously, the code would behave the same.

2) You can't return values from a constructor. Perhaps they meant getting the instance variable values?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the code would behave differently with this. prefixed to ma etc. But I would regard both versions as incorrect.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Perhaps they meant getting the instance variable values?


Yes, absolutely !! How to do it ?
 
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:What diffrence would happen If I add  ' this. ' to 25,26,27,28 line?
Variables on line no 25-28 are local variables but not the instance variables which are declared on line no 19-22 because local variables( method parameters) having same name akin to instance variables on line no 19-22, so instance variables gets hidden by these local variables of constructor of class Mark.

Keyword this is a reference to the current object so when these local variables prefixed with this. then it would refer to the instance variable of current object.


Yes, absolutely !! How to do it ?

This might help you --->Returning a Value from a Method
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got new questions again.

I have tried to create object, why compilator shows it as a mistake?





So finally i done this. I print values to console, but I used to method here? How need to use method to return values?







 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
I have tried to create object, why compilator shows it as a mistake?


Perhaps I am missing something here ... but basically, you tried to instantiate your Mark class, but didn't pass in any arguments, and hence, got a compiler error. And your solution was to remove the arguments (and configuration capability) from your Mark class?

Henry
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

And your solution was to remove the arguments (and configuration capability) from your Mark class?

Henry


I had to clear parameters in 'Mark' constructor
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this counts as a method? If not, how it is named?

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is a valid method signature. (It's not a method because it's missing the code which would be the body of the method, though.)
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:I had to clear parameters in 'Mark' constructor
Then did you think about how you are going to set different values of instance variables ma and mod than "marke" and "model" respectively?

Does this counts as a method? If not, how it is named?
That is the signature of main method(Special method which is an entry point of program execution) but missing It's body. Worth reading click here--->Defining Methods
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, since the homework assignment was to have a method that "return values from constructor to 'running class'", I doubt that the instructor was referring to the main() method (which is a required starting point for the application), when describing about having a method that you "need to write".

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!