File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
Niala Nirell
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
yu yong
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Afzal Hossain
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Nageswar Kakolla
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, it's more clear now
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know, Instance member is called the Class member.
Correct me if i am wrong..
 
swapnil vedpathak
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Gayathri Sugavanam
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic