• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why static methods can't be defined inside Inner classes

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I came through a question in java blackbelt.com


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

Regards
Padma
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because (non-static) inner classes are treated just like any other instance member.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention that you can however, use a static final instance variable. e.g.

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods can only be declared in static or top lavel classes
[ June 27, 2007: Message edited by: Jim Yingst ]
 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes AtulKumar Gaur

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

Regards..
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic