• 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

Member class can have static variables??

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can an member class have a static member??
That is,
class A{
class B{ static int i;}
}
Is this legal??
Thanks,
RCT
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes...
------------------
 
Suresh Chidambaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

IF only Life were that simple..
(SIGH...:-) )
My compiler screams at the static variables in C and B in the following code:
**********************************************************
public class testinner {
static int i=200;
static int j=300;
static class A{ A(){ System.out.println(i++); } }
class B{ static int i;B() {; System.out.println(i++); } }
class C{ static int i=50; C() { System.out.println(i++);System.out.println(testinner.this.i++); } }
public static void main(String args[]) {
A a = new A();
testinner.B b =new testinner().new B();
C c = new testinner(). new C();
A a1 = new A();


}
}

**********************************
The noise it makes I reproduce below:
C:\java>javac testinner.java
testinner.java:8: Variable i can't be static in inner class testinner. B. Only
members of interfaces and top-level classes can be static.
class B{ static int i;B() {; System.out.println(i++); } }
^
testinner.java:10: Variable i can't be static in inner class testinner. C. Only
members of interfaces and top-level classes can be static.
class C{ static int i=50; C() { System.out.println(i++);System.out.println(testi
nner.this.i++); } }
^
2 errors
Besides, why doesnt it complain about the expression
"C c = new testinner(). new C();"
Shouldnt it be" testinner.C=..."
as it is being called from main?/
Java 1.2.2 is the compiler i am using
Stumped...please can someone clarify things as they stand regarding inner classes, in the context of SCJP??
(i.e) Do I take my Compiler's behaviour as normal and believe staunchly that member inner classes can have static member variables or do I follow the general belief(which might, for all I know, be wrong), as proved by my fellow greenhorn's assertion that it is legal, that they can??
And why is the Creation of a C object without using the exprtession "testinner.C=.." not objected to by the compiler?/
Thanks for your replies in advance..
RCT

 
Suresh Chidambaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same results with 1.1.3..
Hmm..I'd rather believe what the two comrades-in-arms say..So, "say nay" to static variables in member inner classses, then..
Fellow Greenhorn bf19314, heed the warning, unless we have conflicting results from brothers 1.1.2, 1.2.1 etc.
Can anyone summarise the rules regarding inner class variables,including local inner classes, please??
Thanks in advance for all your help
RCT
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
non-static inner classes cannot have static data, static fields, or static inner classes. However, static inner classes can have all of these:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Non-static nested classes cannot declare static members. However they can inherit them from other classes.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
non-static inner class may have static members if they are declared as final.. try it in jdk1.2.2
jasjava
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U can make an instance of the inner class as follows :
inner i1=new outer().new inner()
when the instance is made in any of the methods including main() of the outer class.
generally u would use
outer.inner i1=new outer().new inner();
if u examine this - "outer.inner" is similar to - when u refer to a file say try.java in a package pack1 as "pack1.try"
u do not need to refer - outer.inner i1 here as the instance is being created in the outer class itself - so no explicit reference is required. even if it being made in a static method like main - no such explicit reference is reqd. The rule is that static methods cannot access non static variables/ methods - it has nothing to do with the creation of a reference.

[This message has been edited by eskay kumar (edited August 19, 2000).]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no not at alll ...
static means a life without atmosphere(object) . they exist with class name .a member needs this atmosphere ..a member class can call or refer 2 static members but cant diclare them bcos static dosent comes under its jurisdiction ..
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic