This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object accessiblity question ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object accessiblity question ?" Watch "object accessiblity question ?" New topic
Author

object accessiblity question ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
why is the object2 not accessible from the main method of class Test. The class someclass is accessible and instantiated fine from the constructor though.....
if object2 is already available ie instantiated then why is not accessible from the main method ?
class Test {
public static void main(String args[])
{Test object1 = new Test();
//object2 not accessible here, why ?
}
Test()
{someclass object2 = new someclass();}
}
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

mark, object2 is local to the constructor and thus out of reach when the ctor's body has finished executing
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
val:
the reason i asked this question was that in awt examples what we do is typically create (rather construct) all the component objects in a constructor. and in the main method we typically instantiate or fire this constructor.
so basically what we do in the constructor is only local to the constructor ! And it will not be available in the main method !!
i thought that we delegate some tasks in the constructor and do some more tasks in the main. please correct me where i am wrong. typical examples are from the awt components, adding components to containers, listeners etc etc.
Originally posted by Valentin Crettaz:

mark, object2 is local to the constructor and thus out of reach when the ctor's body has finished executing
HIH
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
mark,
think about what happens when, in the constructor, you add a component to a container, for instance, a Button to a Frame.
When the ctor finishes, the Frame is still there and still holds a reference to the Button you added to it, that is, the Button object is not lost because it is referenced by the Frame. You can access that Button by asking the Frame to return a reference to it.
In the example you provided, you just created object2 without any further handling, without making some other object pointing to it or without having a class-wide container object (Vector or something) that would keep a reference to it. A ctor is just like another method in this respect, all local objects created within it, go out of scope once the body has finished unless you ask some other object in the class scope to keep a reference to it.
Here is a different program in which object2 is not lost.

HIH
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi mark,
i guess u want to ask abt the scenario in which we create NEW awt objects and add it to the container in the constructor. right? like this...
containerClass () {
Button b = new Button("button");
add(b);
}
here,
we use "add" method to add components in the panel or whatever the container is and so the object has 2 references after we add it. one reference gets destroyed when the constructor returns but the other remains as it is.
hope i did understand what u wanted to ask.
regards
maulin.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: object accessiblity question ?
 
Similar Threads
Please help me !!!!!answers for sun sample questions
Object and Abstract, Non- Abstract Classes & Methods
passing arguments to a constructor
super ... from JLS ???
Java Free Blocks