• 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

In the static innrer classes

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile the following code

Its giving "Qualified new static class" as compiler error message.What is this mean? Cant we instantiate static class?
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can instantiate a static nested class. In this case, just do

A.B ob = new A.B();
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.Then what does the error mean?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

What are those semicolons...What do they imply??

class A
{
static class B
{
int i=10;
};
};
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shankar kumar,
Its fine working with semicolons at the end of class or even at the end of methods, That is not worth as much, My problem is another thing regarding to the error message I got there.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveen ,
Consider static inner class as a static member of the the enclosing class.Remember the static methods which can be accessed using the ClassName.methodName() syntax.Also the static members of a class are not associated with the instances of the enclosing class.So the syntax

is incorrect.It should be


Jothi replied
What are those semicolons...What do they imply??


You can add as much as semicolon in the source file both inside and outside the class/interface declaration(if I am not wrong). They will be considered as a null statement.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
none of them compiles to me: A.B ob= A.new B();, nor A.B ob= new A.new B();
what is wrong?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
outer class object is never required to access the static member of that class.
so we need not create object for outer class ,to create object for static inner class .

the doubt is why the outer class name is included in the inner class object creation.

to locate the inner class , outer class name just used before inner class name.

we can import the outer class , instead of giving the outer class name before inner class.

inner class file is created like outerclassname$innerclassname.class
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Naveen,

I guess this is the meaning of the error

Qualified new of static classes contradict the specification, which requires that the class being instantiated via a qualified class instance creation.

Static nested classes and true inner classes share many common code paths in the compiler, so this kind of oversight is not unexpected and some times this type of innerclasses may cause incorrect compiler code generation.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Mone:
none of them compiles to me: A.B ob= A.new B();, nor A.B ob= new A.new B();
what is wrong?



Do this:
A.B ob=new A.B();
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that message is for qualified "static member in the class is accessed with instance" ,Then what about the following code "i " is also a static member..right !
class Test
{
static int i=10;
public static void main(String[] args)
{
System.out.println(new Test().i);
}
}
Any way I think static classes are not members.But why they are for? and what they are?
 
Your mother was a hamster and your father was a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic