• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nero patt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nero patt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in fact, the ClassZ instance is created inside the init method of Servlet1, which has "public" access.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
nero patt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, here is the formatted CODE --
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic