File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object accessiblity question ?

 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mark, object2 is local to the constructor and thus out of reach when the ctor's body has finished executing
HIH
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic