Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

nested class method

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
This is my favorite tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic