• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in fact, the ClassZ instance is created inside the init method of Servlet1, which has "public" access.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, here is the formatted CODE --
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic