Nested Classes & Acessibility
I have a class A, which can contain multiple instances of B.
Class A
Class B
HashMap MapofSomeIdAndObjectsOfB;
My Query is:-
Class C
Can I take reference of Class B here (which is a nested class in class A)...If yes, how?
}
A.B b = new A().new B()
Similar Messages
-
Suppose an interface happens to have an inner class or nested class inside its definition.
If a class implements this particular interface, will this class have acess to the methods of
nested class in the interface, OR will this class only have access to the methods in the interface.It could make sense for an interface to define a nested exception type to be thrown by one of its methods.
E.g.
interface MyInterface {
void handleSomething(...) throws OopsException;
class OopsException extends Exception { }
}This is the only circumstance I recall where I've defined a nested class in an interface. When all implementations of the interface are supposed to use this exception type, and it's not supposed to be thrown from anywhere else, it just made more sense in terms of naming and scoping than making the exception a top level class. -
Question about the property of the nested class.
"Nested classes may be declared static. In this case, the nested class has no reference to an enclosing instance"
Can any one demonstrate the above through an code example?? thanksthanks jverd.
Then why dont you answer my other thread??You have several threads. I've said what I have tosay in all the ones that I know about, am interested
in, and have the time to answer. Last one I lookedat, you were in Monica's very capable hands, so I had
nothing to add.
Thanks!I may not be the sharpest knife in the drawer, but I know better than to piss of her what's gonna administer the dread Blueberry Pie Torture. -
Flex RPC mapping Java nested classes to ActionScript classes
We are calling a Java method in our project using
RemoteObject the value returned by the java method is a ArrayList
containing instances of a class say ParentClass with some nested
classes.
The structure of ParentClass(Java) is something like this
class ParentClass {
//some primitives
private ChildClassA childAInstance;
private ChildClassB childBInstance;
//getters setters
We have created similar class structure on the ActionScript
side, the ActionScript classes(ParentClass,ChildClassA
,ChildClassB) have been properly annotated with the [RemoteClass]
metadata tag,
The problem is that though i'm getting the primitive data
members of the ParentClass through RPC in my corresponding AS
ParentClass class i'm getting an runtime exception trying to access
ChildClassA,ChildClassB members.
Am i missing something, exactly the same scenario is
mentioned in this article
http://www.adobe.com/devnet/flex/articles/complex_data.html
But the Object.registerClass method mentioned in the tutorial
is giving me compilation error, the sample code attached with the
article is corrupt zip too.
Please help me out on thisJAXB will create classes from an XML schema, SAX is a parser library and JAXP is a library of XML a bunch of XML tools.
I don't care for JAXB too much. I would skip it and go right to the JAX-RPC spec (WebServices). -
Nested Classes in Labview 8.2
hello everyone,
I am a 'measurement studio' convert to labview, so I have been getting my head around the 'object oriented programming' features of labview 8.2. There seems to be many things lacking in this implementation, like Java/C# style interfaces, and abstract classes.
Also I haven't found a way to create a nested class (so a class within a class). Is this possible in Labview 8.2 or 8.5?
Thanks,
paulI just stumbled across this post looking for other information......
Is it true that nested classes are not supported?
I just tried this out and it seemed to work for me. Can we add a class as a data member of another class? I think I recall this even being propagated in an article, but I can't remember which.
I think it was about an object referencing itself in it's own data field to create a recursive object call.....
Can someone please confirm that nested classes are supported and working? Which caveats need to be observed? I'm just starting working on something which kind of relies on the ability to nest classes, so I'm quite interested as to whether it's feasible or not.
I'm using LV 8.2.1.
Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2) -
Nested Classes and Static Methods
I was perusing the Java Tutorials on Nested Classes and I came across this...
[http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html|http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html]
I was reading the documentation and I read a slightly confusing statement. I was hoping some further discussion could clarify the matter. The documentation on "Nested Classes" says (I highlighted the two statements, in bold, I am having trouble piecing together.)
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class ? it can use them only through an object reference.
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
So this means a nested static inner class CANNOT refer to its own instanced variables, like a normal static method, but CAN refer to its outer class instanced variables?So this means a nested static inner class CANNOT refer to its own instanced variables, like a normal static method, but CAN refer to its outer class instanced variables?No, it means that a static nested class cannot refer to instance variables of its enclosing class. Example:public class Foo {
int i;
static class Bar {
int j = i; // WRONG! Bar class is static context
}~ -
Nested classes and separate files in Javadoc...
Hello. I am writing an application, and it has several .java files, and some of the classes contained therein contain nested classes, for example
ClassOne.java
class ClassOne
// stuff
class innerClassOne
//more stuff
}ClassTwo.java
class ClassTwo
// stuff
class innerClassTwo
//more stuff
}What I want to do is generate documentation like the online java documentation for my app, that is, documentation that will contain ALL the classes and nested classes involved, and link everything that comes in java already to the sun online java docs.
I've figured out the -link command for javadoc, and I can make that work, but I've run into some other snags.
1.) Javadoc isn't finding or documenting my nested classes, despite them being marked up. Google does not wish to help.
2.) Some classes reference other classes of mine that aren't conatined in the same .java file, but do exist in their own .java files in the dame local directory. Javadoc does not like this, and gives errors.
3.) Running javadoc on one file at a time overwrites the old file's documentation, thus I cannot seem to get an index.html javadoc file that links to all my classes, nor can I get the classes to link to each other.
What do I need to do to fix these?So this means a nested static inner class CANNOT refer to its own instanced variables, like a normal static method, but CAN refer to its outer class instanced variables?No, it means that a static nested class cannot refer to instance variables of its enclosing class. Example:public class Foo {
int i;
static class Bar {
int j = i; // WRONG! Bar class is static context
}~ -
JDWP reference implementation does not return anonymous nested classes?
Using
$ java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
it appears that when I connect with JDWP and issue a NestedTypes command, the result does not include anonymous nested types. The only references to this that I could find is a comment in a svn commit at apache (http://mail-archives.apache.org/mod_mbox/harmony-commits/200611.mbox/%[email protected]%3E)
Is this intentional and desired? Is there a way to get all of the nested types, including the anonymous ones? I could do ClassesBySignature with "package.ClassName$*" as the signature and filter out doubly nested classes, but that seems overly complicated.exept, you can NOT have an implementation of an
abstract class (which foundPlugin) is without an
implementation of all it's subclasses (such as
argsObject) You're mistaken in a couple of ways here.
First, a nested class is not the same as a subclass.
Second, a concrete class must have implementations for all its methods, but it can still have an abstract nested class.
Third, you can have an instance of a concrete class that has abstract subclasses. Object is concrete and has many concrete subclasses. Classes don't know anything about their subclasses.
This compiles.
public abstract class AbOut {
public abstract class AbIn { public abstract void bar(); }
public class ConcOut extends AbOut {
public abstract class AbIn2 { public abstract void foo();}
foundPlugin MUST have an implementation
of argsObject, according to the rules of java,And you think this because...?
You read it in the JLS? Citation please.
You tried to compile and it failed? My sample above demonstrates the countereample. If I'm misunderstanding what you're talking about, please post an example
Or maybe you just thought it had to be that way because that seemed the most intuitive and when it wasn't, rather than trying to understand or adjust your thinking, you figured it would be more fun to throw a hissy fit and call Java "stupid"? -
Incorrect ClassDoc for nested classes
Hello,
I think there is a bug in Javadoc concerning nested classes ClassDoc records extracted through the doclet API.
In particular, ClassDoc records are correct if the container class is directly specified on the command line as a .java file or part of a package (i.e. the source is available), but they are (at times) wrong if the records are built from .jar (.class) files through the classpath.
Example with "java.io.ObjectOutputStream" from JDK 1.3.1_01:
If the ClassDoc for this class comes from RootDoc.specifiedClasses(), its nested classes are correctly listed as:
private static final class java.io.ObjectOutputStream.HandleTable
public abstract static class java.io.ObjectOutputStream.PutField
static final class java.io.ObjectOutputStream.PutFieldImpl
private static final class java.io.ObjectOutputStream.ReplaceTable
private static final class java.io.ObjectOutputStream.Stack
but if the ClassDoc of the container class is extracted from a method parameter, like in org.apache.xerces.dom.ParentNode.writeObject(ObjectOutputStream out), this is the result:
final synchronized class java.io.ObjectOutputStream$HandleTable
public abstract static class java.io.ObjectOutputStream.PutField
static final class java.io.ObjectOutputStream.PutFieldImpl
final synchronized class java.io.ObjectOutputStream$ReplaceTable
final synchronized class java.io.ObjectOutputStream$Stack
According to my tests, at least ClassDoc.modifiers(), ClassDoc.qualifiedName() and ClassDoc.containingClass() return incorrect results for HandleTable, ReplaceTable and Stack.
This behaviour seems common to both Javadoc 1.3.1_01 and 1.4beta3. I admit I didn't test 1.4 final, but I searched the Javadoc site, the forum and the bug database without finding anything similar.
Thanks in advance for any help.
Amedeo FarelloThank you for looking into this.
This is beyond my understanding of the Doclet API.
Please submit this as a bug using the instructions at:
http://java.sun.com/j2se/javadoc/faq/index.html#submitbugs
and please email the ID number you get back to me
at [email protected] so I can make sure it gets
past our initial reviewers and into our bug database so
our javadoc engineer can look at it.
-Doug Kramer
Javadoc team -
Hi, i currently have the following problem:
I have 2 classes, one of which also has a nested class:
1. Home.java (extends JApplet)
2. Away.java (extends JPanel) has nested class, private class ButtonListener implements ActionListener
Now in Home.java I have a Vector ( called bankaccount) which i pass to the Constructor of Away.java ( I declared the vector again there as an instance by Private Vector bankaccount;).
My problem is that in ButtonListener I am trying to access the Vector so that i can modify it when a button has been clicked, and i thought i could just use the commands of bankaccount.add() and bankaccount.get() directly, however it seems like ButtonListener cannot access the Vector, even though i passed it along to the Constructor of the Away class.
Any suggestions on how i get access the Vector from the ButtonListenere class?This is an example:
public class Home extends JApplet
private Away away;
private Vector bankaccount;
public void init()
bankaccount = new Vector();
away = new Away(bankaccount);
public class Away extends JPanel
private Vector bankaccount;
JTextArea text1= new JTextArea(50,50);
public Away(Vector bankaccount)
//declare buttons,textarea,setLayout,etc
button1.addActionListener (new ButtonListener());
//i can set text1.setText(( String)bankaccount.get(0));
// here to access bankaccount, and it shows up fine, but i want
// do it in from the class below
private class ButtonListener implements ActionListener
public void actionPerformed(ActionEvent event)
//trying to access bankaccount here by doing:
// but it is not doing anything
text1.setText(( String)bankaccount.get(0));
} -
Questions concerning Nested Classes
Hi!
Just read some articles about nested classes (include the ones from the Java Tutorial and the Effective Java chapter), and while most of it is perfectly clear, three questions remain:
1.) When I declare a member class, how should I declare the access specifiers (private, protected etc.) for the member class's attributes and methods? If I declare a member class as private, anything but private for the attributes and methods wouldn't make sense, would it?
2.) The Java Tutorial says:
Also, because an inner class is associated with an instance, it cannot define any static members itself.While this is true for static methods, it seems to be possible to declare static variables inside inner classes. This is confusing me... does it actually make sense to declare static variables inside inner classes (or member classes in general)? Or should the be placed in the declaring class?
3.) Another confusing quote from the Java Tutorial:
Static nested classes do not have access to other members of the enclosing class.This is true for instance variables and methods but not for static variables, which are also members of the enclosing class, aren't they?
Thanks in advance,
OIiverTrollhorn wrote:
Hi!
Just read some articles about nested classes (include the ones from the Java Tutorial and the Effective Java chapter), and while most of it is perfectly clear, three questions remain:
1.) When I declare a member class, how should I declare the access specifiers (private, protected etc.) for the member class's attributes and methods? If I declare a member class as private, anything but private for the attributes and methods wouldn't make sense, would it?
private static class MyInner implements Runnable
@Override public void run() // Must be public!
2.) The Java Tutorial says:
Also, because an inner class is associated with an instance, it cannot define any static members itself.While this is true for static methods, it seems to be possible to declare static variables inside inner classes. This is confusing me... does it actually make sense to declare static variables inside inner classes (or member classes in general)? Or should the be placed in the declaring class?Wrong. It can define static final member variables and I see no reason to move them to the outer class, if they are used only by the inner class.
3.) Another confusing quote from the Java Tutorial:
Static nested classes do not have access to other members of the enclosing class.This is true for instance variables and methods but not for static variables, which are also members of the enclosing class, aren't they?I agree with you here.
Edited by: baftos on Jun 6, 2009 9:18 AM -
Compiling Nested Classes and Interfaces
I am looking for documentation about compiling nested classes and interfaces. I have found something in the JVM Specification, but there does not explain how a nested class is compiled and what is included in the top level class to mark a "place holder" to the nested class. The JVM Specification in this topic cite the web page http://java.sun.com/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc.html that does not exists any more.
My root problem is that: I am compiling a class with a private nested class, but in the class file generated TopLevelClass$NestedClass.class the class does not have the private modifier. So I am not understanding why the "private" modifier was removed during compilation.
I performed the same test with a protected nested class and the result was the nested class with the public modifier. So I am not understanding why the "protected" modifier was changed to "public".
Thanks in advance,
Mardoqueu.This should not be happening. What compiler are you using? If it's a reasonably recent Sun compiler, could you post a minimal example?
-
How to ensure multiple nested classes static readonly initialization orders
Hello,
I am emulating the Java Enumeration in a first class class, with static readonly field initialization.
In an effort to further categorize some instances, I embed them in nested classes. Something like this.
class A : Enumeration
public static class CatB
public static readonly A FirstInstance = new B();
public static readonly A SecondInstance = new B();
public static class CatC
public static readonly A ThirdInstance = new C();
public static readonly A FourthInstance = new C();
class B : A
class C : A
So far so good.
One strain of Enumeration has built-in Ordinals, so I initialize them in the static ctor, or I make an effort to.
static A()
var values = GetValues().ToList();
var ordinal = 0;
values.ForEach(v => v.Ordinal = ++ordinal);
Also, in places, so far so good.
However, in a main method, I am referencing one of the enumerated instances, so calls to my GetValues() is returning nulls for that nested class. The other nested class instances are just fine, they return just fine.
I could post more, but I think this illustrates the idea.
I am suspicious that a strange static initialization order is jumbled up, as evidenced by nested static ctor debug statements I put in. I get one print and not the other.
When I switch up which instance I reference in my main, I get the opposite behavior, more or less confirming my suspicions.
So... Question is, what's the best way to potentially work around this issue? Hopefully without abandoning the Enumeration concern altogether, but that thought is a possibility as well. Or at least do so differently.
Thank you...It is probably necessary to show GetValues() at least. I am getting the correct nested types, so that much we can at least assume.
protected static IEnumerable<TDerived> GetValues()
var declaringTypes = GetDeclaringTypes(typeof (TDerived)).ToArray();
foreach (var declaringType in declaringTypes.Reverse())
var fis = declaringType.GetFields(PublicStaticDeclaredOnly);
var values = fis.Select(fi => fi.GetValue(null)).ToArray();
foreach (var value in values.OfType<TDerived>())
yield return value;
Okay, here a bit of explanation.
Enumeration is declared generically, with TDerived as the derived type. In this case, A.
GetDeclaringTypes is operating okay. I get types for A, B, and C.
The fis to values is breaking down, however. When I reference any of the CatB's, I get nulls for those instances. Conversely, same for CatC's when I reference any of those. -
Breakpoints in nested classes don't work
I've noticed that when I set a breakpoint in a nested class, it doesn't work to stop the code, unless I set the breakpoint after I've created an instance of the class.
In the code example below, if I set breakpoints A, B, C and D, only breakpoints A and C will be hit when MainClass.main runs.
However, if I set breakpoint D once breakpoint C is hit, then breakpoint D works.
I've noticed this behavior in both JBuilder 4 and Eclipse, which are both using the Sun 1.3.1 JRE/JDK.
Does anyone know if there is a way to get breakpoints in nested classes to work as expected (they stop the thread whenever that point in the code is executed, and setting them before the class is loaded/instantiated doesn't change that behavior).
BTW, in my research on this topic, I found a mention of "class preparation", a step that the JVM goes through when loading a class. I wonder if the class has to go through this step before the breakpoint can be enabled.
--James
public class MainClass {
public static void main(String[] args) {
Object o = OtherClass.Test(3); // Breakpoint A
System.out.println(o);
class OtherClass {
static Object Test(int v) {
Object o = new NestedClass(v);
return o; // Breakpoint C
static class NestedClass {
int x;
NestedClass(int v) {
x = v; // Breakpoint B
public String toString() {
return "NestedClass(x=" + x + ")"; // Breakpoint D
}You wrote:
-> I've noticed this behavior in both JBuilder 4 and Eclipse, which
-> are both using the Sun 1.3.1 JRE/JDK.
Take a look at Bug-ID 4299394
Synopsis: TTY: Deferred line breakpoints can't be set on inner classes
http://developer.java.sun.com/developer/bugParade/bugs/4299394.html
-> BTW, in my research on this topic, I found a mention of "class preparation",
-> a step that the JVM goes through when loading a class. I wonder if the class
-> has to go through this step before the breakpoint can be enabled.
A breakpoint cannot be set in a class before the class prepare event for
that class. Also: the fully qualified name for the inner or nested class
must be constructed properly.
Any breakpoint requested before the target class is loaded must be 'deferred',
which means the debugger keeps track of the breakpoint request and sets
it when the class load eventually occurs.
-> However, if I set breakpoint D once breakpoint C is hit, then
-> breakpoint D works.
By the time you get to the breakpoint at C, the inner class containing D
(OtherClass$NestedClass.class) was located, loaded, and prepared as required
by the "Object o = new NestedClass(v);" statement. -
When to use Nested Class/Inner Classes ?
I am not very clear, when to use nested/inner classes..
The scenario is :-
class ABC
//ABC need to store multiple instance of class XYZ
class XYZ
int member1;
int member2;
One approach is
class ABC
class XYZ
//vector of class XYZ instances is stored in class-ABC
or another approach is Class XYZ can be in separate JAVA file.
Query:-
1) Is there any difference between nested or Inner class...or are they same?
2) When should they be used....Is it good to use in above scenario.
3) What are the disadvtanges/advantages of using the Nested
class.Query:-
1) Is there any difference between nested or Inner
class...or are they same?I really don't get it. Yes there is a difference between having an inner class, and a class in a separate file, but a nested class is an inner class.
2) When should they be used....Is it good to use in
above scenario.To write an inner class is a design decision. Do other classes need to know about the class XYZ or not? Do the XYZ class need to know about the inner working of the ABC class? How complex is the XYZ class etc.
>
3) What are the disadvtanges/advantages of using the
Nested
class.See above.
/Kaj
Maybe you are looking for
-
Neowiz duel poker won't open on iPhone 5. I used it on iPhone 4 s. any solutions?
-
Hi All, I have a table which has around 50 million rows. I want to update a particular column for all the rows in the table based on some join conditons with other tables. the conventional update method is taking too much time. no matter if i use a i
-
Invoice details in tables BSEG,BSID
Hi Experts, 1.How to get the details of open invoice(customer invoice) from table BSEG, BSID or BKPF. 2.Which field in these table belongs to invoice number? 3.Will accounting document gete generated for every invoice created & cancelled? I dont want
-
Qualification catalog translation
Hello , We are planning for translation of catalog in German, French and some other languages. Can anyone please help me with the questions below ? 1. How do we translate the catalog (QK, Q, Proficiency scales , descriptions ) if the language of crea
-
Question on billing location, etc
Hey! Is the incoming call originating in Jupiter or is it where I received it? The call I placed, it's originating in Tequesta, which is obvious. How accurate are these locations? The phone is an old dinosaur/flip phone. Thanks for you input. Date Ti