Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

best coding practices

 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which type of coding is efficient or best practice :



Note : we are not using reference of MyClass outside of this method .

thanks a lot .
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The latter. Encapsulation is of utmost importance.
Do not expose the reference to a greater scope than it needs to be.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first example, 'MyClass' object will be eligible for GC iff:
1) You explicitly set its reference to null.
2) Class 'Other' is EGC

However in the second example the object will be eligible for GC as soon as m() method exits.

From Memory-Usage point of view, i'll go with the second code sample.
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek ,
In first case also , object will be eligible for GC as soon as the method will get complete .
[ February 07, 2005: Message edited by: rathi ji ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
Vivek ,
In second case also , object will be eligible for GC as soon as the method will get complete .


Yes that is true, but the real question is how soon the object will be EGC. Get it?

It is really frustrating how people misspell my name , it is not Vicky, Vivek, Velder, or any other awkward combination. It is simply pronounced V-I-C-K-E-N, think of it as a combination of Vicky and Ken (just drop the y). Thank you for your time and happy ranching.

My apologies rathi if i sounded harsh, nothing personal is intended.
[ February 05, 2005: Message edited by: Vicken Karaoghlanian ]
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes that is true, but the real question is how soon the object will be EGC. Get it?



Not vicken ... please help .

thanks .
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer to the following link, which explains the differences between instance variables and local variables:
web page
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In first case also, object will be eligible for GC as soon as the method will get complete.


I don't think so.

In the first case, the myClass object will last at least as long as the Other object. In the second case, the myClass object will be eligible for collection as soon as method m() returns and the stack frame holding ob is discarded.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike is right. Instance members can't get GC'd until either the instance they belong to gets GC'd or they are explicitly set to null.
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means , we can use that reference outside that method code as well .

Please comment .

thanks .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, instance variables are available to any method in the class as well as by any other class which has access to members with the relevant access privileges for that instance variable.

Automatic variables are available only to the method in which they were created (or in very specific cases to instances of inner classes defined and created within that same method).
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Automatic variables
That's not a Java term. Are you thinking FORTRAN 90 or PL/I or ???
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic