aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes nested class method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "nested class method" Watch "nested class method" New topic
Author

nested class method

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
hi!
can anyone tell why the line 2 is giving error
<code>
public class Nested1
{
public static void main(String args[])
{
new Nested1().new Nested().method("first"); //1
//new Nested1().Nested.method("second"); //2
Nested1.Nested.method("third"); //3
}
static class Nested
{
static void method (String str) //4
{ System.out.println(str); }
}
}
</code>

madhur.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The Java 1.3.1 compiler on MacOS X 10.2 gives the following error when I uncomment that line and compile:

% javac Nested1.java
An exception has occurred in the compiler (1.3.1). Please file a bug at the Java Developer Connection (http://java.sun.com/cgi-bin/bugreport.cgi). Include your program and the following diagnostic in your report. Thank you.
java.lang.NullPointerException
at com.sun.tools.javac.v8.comp.TransInner.access(TransInner.java:743)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1578)
at com.sun.tools.javac.v8.tree.Tree$Select.visit(Tree.java:963)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1572)
at com.sun.tools.javac.v8.tree.Tree$Select.visit(Tree.java:963)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1503)
at com.sun.tools.javac.v8.tree.Tree$Apply.visit(Tree.java:785)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:179)
at com.sun.tools.javac.v8.tree.Tree$Exec.visit(Tree.java:699)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:47)
at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:111)
at com.sun.tools.javac.v8.tree.Tree$Block.visit(Tree.java:492)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:100)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1436)
at com.sun.tools.javac.v8.tree.Tree$MethodDef.visit(Tree.java:441)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1373)
at com.sun.tools.javac.v8.tree.Tree$ClassDef.visit(Tree.java:402)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner.translate(TransInner.java:1335)
at com.sun.tools.javac.v8.comp.TransInner.translateTopLevelClass(TransInner.java:1603)
at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:397)
at com.sun.tools.javac.v8.Main.compile(Main.java:247)
at com.sun.tools.javac.Main.main(Main.java:16)


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104

For me both the lines 1 and 2 result in compiler error(JDK 1.4)
line 1 does not require the instance of the enclosing class to call the method of the static inner class.it works fine without that.

same is with line 2
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The class Nested is static, so it does not need an instance of the containing class to created an instance of it.
Both of
new Nested1.Nested().method("first"); // create instance of Nested and calls first.
new Nested().method("first"); // ditto
compile.
Ron, yes, well, ..., dunno. I compiled on intel with Java 1.4 and it just gave me an error about "qualified new of static class".
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Richa Jeetah
Greenhorn

Joined: Sep 27, 2001
Posts: 29
Hi
Did anyone get some more explanation on this program ?
i experimented a bit more with the given program and found that:
comenting lines 2,4 eleminates the erroes during compilation as mentioned by our friend 'Ron Newman'.
i ran this program:
public class Nested1 {
public static void main(String args[])
{
new Nested1().new Nested().method("first"); //1
//new Nested1().Nested.method("second"); //2
Nested1.Nested.method("third"); //3
//Nested1.new Nested().method("third"); //4
Nested.method("fifth"); //5
new Nested().method("sixth"); //6
}
static class Nested {
static void method (String str) //4
{
System.out.println(str);
}
}
}
on running this i got :
first
third
fifth
sixth
I know that since 'Nested' is a static class so it can be called independently from Nested1 class.
Does not require an instance of Nested1 class.
Am i right?
thanx
Richa
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154

JDK 1.2.2 :-
Compiles successfully, BUT on execution -
Exception in thread "main" java.lang.VerifyError: (class: Nested1, method: main signature: ([Ljava/lang/String V) Expecting to find unitialized object on stack
JDK 1.4.1 :-
Nested1.java:5: qualified new of static class
new Nested1().new Nested().method("first"); //1
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

The above code worked for me on 1.3 compiler. Note, I have made some changes. Seems like that creation of outer class object on the fly (without assigning a reference to it) causes grief...
[ September 09, 2002: Message edited by: Barkat Mardhani ]
Fintan Conway
Ranch Hand

Joined: Apr 03, 2002
Posts: 141
Hi Madhur,
In line 2 the compiler is treating the expression after the word "new" as an expression and evaluating the expression before trying to create a new object. i.e. Nested1().Nested.method("second").
It then finds that the evluated expression does not correspond to a class that it can create a new object from.

Originally posted by madhur jain:
hi!
can anyone tell why the line 2 is giving error
<code>
public class Nested1
{
public static void main(String args[])
{
new Nested1().new Nested().method("first"); //1
//new Nested1().Nested.method("second"); //2
Nested1.Nested.method("third"); //3
}
static class Nested
{
static void method (String str) //4
{ System.out.println(str); }
}
}
</code>

madhur.

[ September 10, 2002: Message edited by: Fintan Conway ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Fintan:

Originally posted by Fintan
In line 2 the compiler is treating the expression after the word "new" as an expression and evaluating the expression before trying to create a new object. i.e. Nested1().Nested.method("second").
It then finds that the evluated expression does not correspond to a class that it can create a new object from.

If what is stated in bold is true, why following code compiles

Thanks
Barkat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nested class method