aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why static methods can't be defined inside Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why static methods can Watch "Why static methods can New topic
Author

Why static methods can't be defined inside Inner classes

Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi

I came through a question in java blackbelt.com


Why can't we put static methods in regular inner class?

Regards
Padma
Kantutan Tayo
Greenhorn

Joined: Oct 07, 2005
Posts: 22
Because (non-static) inner classes are treated just like any other instance member.


SCJP
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hi Padma,

yes you are right i get a compiler error, if i remove the static keyword from the static inner class method then it complies just fine, i don't know what is the reason but i am also curious to know the solution or the reason.
Kantutan Tayo
Greenhorn

Joined: Oct 07, 2005
Posts: 22
I forgot to mention that you can however, use a static final instance variable. e.g.

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Top-level classes can contain both instance methods and static methods, but apparently, inner classes can only contain static methods if the inner class is labeled as a static class.

I labeled the inner class as static and left the static modifier on the method of the inner class and it compiled and ran.

Top-level classes cannot be labeled using the static modifier.

I don't know why this needs to be this way.

Kaydell
Rama Lakshmi
Greenhorn

Joined: Mar 31, 2006
Posts: 27
Hi,

Why can't we put the top level class as static such as

public static class OuterClass {

static {
}

public static void main(String[] args) {

}

}


Regards
Rama
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Padma Asrani

Why can't we put static methods in regular inner class?


We can't have a static method inside a regular inner class..
while you can have them inside top level nested classes..

It's because regular inner classes always need an instance of outer classto get themselves instantiated..

However static methods don't need an instance of the class..
They are associated with the class not the instance..
Hence you get a compile time error..

Hope you might have got it..

Regards..


rgrds,
Khushhal
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
static methods can only be declared in static or top lavel classes
[ June 27, 2007: Message edited by: Jim Yingst ]
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Yes AtulKumar Gaur

You got it..
That's the only place where you can have static methods..

Regards..
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi khushhal,

Thanks for your reply.


We can't have a static method inside a regular inner class..
while you can have them inside top level nested classes..


I know that because I had main method in the Outer class



It's because regular inner classes always need an instance of outer classto get themselves instantiated..

However static methods don't need an instance of the class..
They are associated with the class not the instance..
Hence you get a compile time error..



I couldn't get this part. Why can't we create the instance of Outer class i.e.
Outer out = new Outer();

and call the static method inside Inner class as

out.Inner.myMethod();

Regards
Padma
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Why can't we create the instance of Outer class i.e.
Outer out = new Outer();
and call the static method inside Inner class as
out.Inner.myMethod();

If you do this, it means you need 'Outer class' object to call static method. However, static members are totally independent of objects as static members belong to class and not object.

That is why inner classes are not allowed to have static methods.
Final-static fields are CONSTANTS and not variables, thats why they are allowed in inner class.


SCJP 5.0, SCWCD in progress
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

The key to a static method, or variable more specifically, is the fact that it isn't tied to any one instance of a class. That's the whole point of a static variable, no matter how many instaces of a given class you have, they all point to the same memory location that stores the value of the static variable. Static methods, of course, work on these static variables.

BUT, with inner classes, the inner class is associated with a given outer instance. So, if you have ten outer instances, should you have ten, different, static memory locations, for each of the inner instances they contain, or one static memory location for each instance? The questions really doesn't have an answer, because either way, the answer is unsatisfactory. Furthermore, when a static method is called, which static variable does it use, especially if there are many outer instances?

This is the crux, or paradox, or problem with inner classes having static variables or methods.

-Cameron McKenzie


Which of the following lines of code must be commented out in order for the code below to compile:

public class TestClass {
public class InnerClass {
static int x=5; //line a
static final int y=10; //line b
public static void testStaticInner() {} //line c
public static final void testStaticFinalInner() {} //line d
}
}
� a) //line a
� b) //line b
� c) //line c
� d) //line d
Options a) c) and d) are correct, becauase they are incorrect lines of Java code.
This nested class is not static, and the rule is, non-static nested classes cannot define any static member variables or methods, with the exception of a static final variable, which is better thought of as a constant in Java, as opposed to an actual static member.
An attempt to compile the code above would generate compiler error messages along the lines of: inner classes cannot have static declarations




Which line of code will trigger a compile error?

public static class TestClass { //line a
public static class InnerClass {
static int x=5; //line b
static final int y=10; //line c
public static void testStaticInner() {} //line d
}
}

 a) //line a
 b) //line b
 c) //line c
 d) //line d
Option a) is correct.
Top level classes cannot be declared as being static. However, an nested class can indeed be declared as being static.
A static nested class can declare both instance variables and methods, and static variables and methods, so lines b) c) and d) would not cause compile errors.
Taking the word static out of the top level class declaration would allow this code to compile.
[ June 26, 2007: Message edited by: Cameron W. McKenzie ]
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi Cameron

Thanks for the explanation. I got it but I couldn't get the second example. Why the top level classes can't be declared static?

So why can't we say



Regards
Padma
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Hey Padma.

I think we're just talking about a fundamental aspect of the keyword static.

The static keyword essentially means 'belonging to the class,' and as such, it can only be applied to member level artifacts, such as member levelvariables, member level methods, and yes, member level classes in the case of nested classes. So, the keyword static is always seen inside of a class, but outside of any methods or types defined within a class.

I'm trying to think up examples that contradict the above statement, but I can't think of any. I'm sure some of the people here who are much smarter and better looking than me might be able to, but I think it's a pretty solid statement.

-Cameron McKenzie
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you hunt around in the Java forums and Java Bug database you can find opinions that static members could be implemented in inner classes but it would mean having a different and more complex class model to that used currently. So I guess (and am glad) that simplicity rules over this issue.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why static methods can't be defined inside Inner classes