Two Laptop Bag*
The moose likes Beginning Java and the fly likes how to reference the class-instance, created in parent class. 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 » Java » Beginning Java
Bookmark "how to reference the class-instance, created in parent class." Watch "how to reference the class-instance, created in parent class." New topic
Author

how to reference the class-instance, created in parent class.

nero patt
Greenhorn

Joined: Aug 29, 2002
Posts: 8
Hi, I have following scenario. During compilation I am gettting error message -- "cannot resolve symbol - symbol : Variable myZ"


CODE :
--------
under package A.1;
ClassZ
Servlet1
ClassZ myZ = new ClassZ;
under package A.2;
Servlet2 extends Servlet1
myZ.printHi();

How to reference the class-instance created in the parent class?
Thanks.
nero patt
Greenhorn

Joined: Aug 29, 2002
Posts: 8
sorry.. the code is not properly visible... it is like this --
under package A.1;
ClassZ
Servlet1
ClassZ myZ = new ClassZ;
under package A.2;
Servlet2 extends Servlet1
myZ.printHi();
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The simplest solution, although not the most elegant, would be to give myZ protected access, rather than private access. (At least I assume myZ has private access at the moment.)

Layne


Java API Documentation
The Java Tutorial
nero patt
Greenhorn

Joined: Aug 29, 2002
Posts: 8
in fact, the ClassZ instance is created inside the init method of Servlet1, which has "public" access.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Obviously fake names and "handles" aren't alowed here. You can change your display name
here.
Thanks!

Regarding the formatting in your posts; you can use the UBB "CODE" tag to preserve formatting of code or code-like text in your posts. Just wrap the code in tags that look like HTML <CODE> tags, but use square brackets "[]" instead of angle brackets "<>".

As to your actual question: based on what you've told Layne, it sounds like myZ is a local variable in a method. A local variable is accessible only inside the method where it's declared; you could turn myZ into a protected member of Servlet1.

Finally, we frown on posting duplicate threads -- please don't do it. I deleted the copies of this you posted in Servlets and in Advanced Java.

It looks like you didn't post a copy in the one forum where this question really belongs: Java in General (Beginner). I'll move this thread there for you.
[ March 21, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
nero patt
Greenhorn

Joined: Aug 29, 2002
Posts: 8
Hi Ernest,
Sorry for the misunderstanding on my part. I have changed the name and thanks for taking care of moving the thread to the right place.

Thanks.
nero patt
Greenhorn

Joined: Aug 29, 2002
Posts: 8
By the way, here is the formatted CODE --
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Right, so that's as I guessed. myZ is a local variable in init(). It needs to be a member variable of Servlet1 -- i.e., defined, although not necessarily initialized, outside of any method -- for Servlet2 to see it.

Of course, depending on what's going on, exactly, this could be a bad idea, because of how Servlet objects are shared. Depending on the situation, it may be better to store the informating in the SessionContext, for example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to reference the class-instance, created in parent class.
 
Similar Threads
how to reference the class-instance, created in parent class.
Access modifier confusion
access modifier
Servlet Concepts
running a servlet without IDE