Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

get reference to container class

  Asked By: Hamish    Date: Jul 04    Category: Java    Views: 1319
  

I need help with the following issue: say I have a class that gets
instantiated inside of another class; I want to know how can I
access this outer class inside the first class. To make the question
a bit more clear, let's say I have:

class b {
//...
}

class a {
b instB = new b()
//...
}

How do I refer to class a from a method of class b?

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Aberto Rossi     Answered On: Jul 04

What are you trying to access?

If you are trying to access some data from the first one, can you pass
it along?

B b = new B(withSomeDataFromAForAConstructor);

or

B b = new B();
b.runSomeMethodInB(withSomeDataFromA);

or a combination of both.

 
Answer #2    Answered By: Nina Garcia     Answered On: Jul 04

I new to java, however from the code I’ve read you need to add a
constructor in your inner class  with a reference  similar to the
following:


Class a {

void a(b context) {


}
}

Then instantiate with

a myA=new a(this);

 
Answer #3    Answered By: Wilbert Patterson     Answered On: Jul 04

What I am trying to do is to have a method  in class  b that will use
the class name of class a. Basically I am writing some kind of a
logger class that will print (for a certain error condition) the type
of the error and the class/method in which was detected; the logger
will be an instance variable of all other classes.
Hope this clarify the issue.
I was thinking about passing the class name as parameter in the
constructor also, but I wanted to know if it is a different way to do
it (i.e. access from class b the class name of the container  class).

 
Answer #4    Answered By: Faeezah Khan     Answered On: Jul 04

You should look into using a logger class  that is already built (such as log4j -
http://jakarta.apache.org/log4j - or the logging api built into java 1.4). They
are
very comprehensive and should meet your logging needs. The approach they use is
not
what you are listing here but you may be happier with the methodology they use
than
the one you are proposing.

Also please note that if you catch any exception you can print out a full stack
trace (names and line numbers of the error) by calling its stack trace method.
This
does not require a logger class although it can be used in conjunction. For
instance:

catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}

The exception class has methods in it that can be used to send error information
to
your log as well.

 
Answer #5    Answered By: Kanya Jainukul     Answered On: Jul 04

When start developing those logging classes I
was too much of a newbie in Java to know about logging in JDK 1.4,
not to mention the logging classes by Jakarta. Meantime I found out
about them (actually I'm using java.util.logging in my project), but
I am still unclear about the question posted here. Is not much of a
problem anymore, more like a curiosity.

 
Answer #6    Answered By: Connie Wallace     Answered On: Jul 04

something like this,

class c1
{
class c2
{
public void printClassName()
{
c1 obj=new c1();
obj.printClassName();
}
}
public void printClassName()
{
System.out.println("Class 1");
}
public void accessingNewClass()
{
c2 obj=new c2();
obj.printClassName();
}
}

 
Answer #7    Answered By: Adelino Fischer     Answered On: Jul 04

That only works for generic data ... You are probably better off passing
information to class  C2 from c1 instead of making a second instance of c1

 
Answer #8    Answered By: Joseph Evans     Answered On: Jul 04

I was not talking about inner classes. Just 2 independent classes
linked by a HAS-A (composition) relationship.

 
Didn't find what you were looking for? Find more on get reference to container class Or get search suggestion and latest updates.




Tagged: