• 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

inner class

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
someone can explain me this :
"no inner class can have a static member"

it's from the scjp FAQ and i don't find any reference of this ine te Kera & Bates book

thank you
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alain:
I think there are some mistakes on it. The code which has a inner class using a statci member can compile and run successfully.

class InnerClass {
static final String A = "A";
}
public class StaticTest {

public static void main(String[] args) {
System.out.println(InnerClass.A);
}
}

The result is : A
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you mean? :

public class Test {

class InnerClass {

static final String A = "A";
}

public static void main(String[] args) {
System.out.println(InnerClass.A);
}
}

Anyway, it is still possible to declare a static member into the inner class.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
"no inner class can have a static member"

Yes, that's true.
I dont see any reason to use static member in inner class. Only Top level and static nested class can have static member.

thanks
Afzal
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my example compile perfectly and you have a static member into an inner class. i still don't understand this afirmation.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

static final String A = "A";

is allowed since static final is way of defining CONSTANTS in java.

whereas static variable is not allowed in inner class.

Try
static String A ="A";
and you will get compiler error since above String A is static variable and is not allowed in inner class due to Instance is required for outer class and static variables are not applicable for Instance classes. Hence Non static Inner classes can not contain static member variables but constants are allowed.

Hope this explanation suffice !
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, it's more clear now
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You mean to say that we can define static variables in subclasses not in Inner class.

please shed some lights on this. I am not undertanding the concept.
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe you will understand with an example:

public class Test {

class InnerClass {

static String A = "A";
}

public static void main(String[] args) {
System.out.println(InnerClass.A);
}
}


this code does not compile. static means that the variable is a class member
and not an instance member. Or in order to access an inner class you need an instance of the outer class, so the concept of static member for an inner class is not valid anymore. It's my understanding.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know, Instance member is called the Class member.
Correct me if i am wrong..
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class variables are not bound to any instance.but instance varibles are bound to object.
class Temp{
int i;
static int j;

}
in above case,

i- instance varible

j- class varible
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends...

I have a doubt with method-local inner class.
I understand that the method-local inner class object doesn't have access to the local variables of the method the inner class is in. This is because the local variables live on the stack and their life is over once the method completes.. But the inner class object may still be alive even after the method completes.
Hence the object doesn't have access to the local variables. But if they are marked as final, the object is able to access it. HOW..? Do the final variables live on the heap like the object?

Kindly explain and thanks in advance for any reply.
reply
    Bookmark Topic Watch Topic
  • New Topic