• Post Reply Bookmark Topic Watch Topic
  • New Topic

Don't need to keep a reference  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.....



In preceding code why you don't need keep reference to instance of class (new Foo().go()).In other words the main() method needs to create the instance only so that
main() method can invoke one of the instance's methods.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. You have answered your own question. That is shorthand for. . . and you never use f again.
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the ccontrl flow.
1.The main method is called .
2.The "new Foo()" creates an object of type Foo().
3.The method is called on the newly made Foo object via ".go()".
4.The go method executes completely.
5.Control returned to main method .
6.The semi colon is encountered ";" ,the object that you created is available for garbage collection.
7."main" method terminates
8.All well that ends well
HTH
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit Rai wrote:
6.The semi colon is encountered ";" ,the object that you created is available for garbage collection.


I can't tell if you're just speaking figuratively, or if you'e really thinking of your code as being interpreted one symbol at a time like this at runtime. It doesn't work that way at all, of course; the end-of-statement symbol is used by the compiler to understand your intent, but it has no runtime representation; there's no semicolon "instruction."
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest rightly pointed out that there is no semi colon instruction .My description was intended to help figure out what exactly is happening logically .I have erred on the technical part though .For better understanding of the language I would suggest that you go through any good Java book ,find them at find here - list of beginers java books.
HTH
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!