Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Certification
»
Programmer Certification (OCPJP)
Inner Class cannot have static method ?
Saibabaa Pragada
,
Ranch Hand
Nov 02, 2010 15:59:04
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Hi, Please advice why inner class cannot have static method.
public class P665_MyOuter02_Ask { class MyInner02 { // The method seeOuter cannot be declared static; static methods can // only be declared in a static or top level type static void seeOuter() { } } }
Stephan van Hulst
,
Saloon Keeper
staff
Nov 02, 2010 16:07:47
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
What would be the point?
Inner classes exist only to perform some sort of helper function for the enclosing class. If you're going to give them static methods, you might as well give the static methods directly to the enclosing class.
Prasad Kharkar
,
Ranch Hand
Nov 02, 2010 20:05:34
(0 cow)
(0 skulls)
1
+Pie
Number of slices to send:
Send
I had done a small program regarding the inheritance and the static field declaration in inner classes
hope this is little useful to you
class InnerStaticInheritance { static class InnerSuperClass extends InnerStaticInheritance{// thia is valid, can extend non static outer class static int x = 56; // static members valid in the static inner class public void doMethod(){ System.out.println("In the static inner class in doMethod " + x); } } class InnerSubClass extends InnerSuperClass { //static int z = 5; non static nested classes cannot have static members public void doMethod(){ System.out.println("In the non static inner class"); } } //static class InnerSubSubClass extends InnerSubClass{ //}// above class does not compile because cannot reference this before the supertype is called public static void main(String[] args) { InnerStaticInheritance.InnerSuperClass a = new InnerStaticInheritance.InnerSuperClass(); // we do not require the instance of outer class to instantiate the static inner class a.doMethod(); } } /// inner static class can extend outer non static class
Saibabaa Pragada
,
Ranch Hand
Nov 02, 2010 21:02:23
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Thanks Stephan & Prasad
Post by:
autobot
money grubbing section goes here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
This thread has been viewed 6471 times.
Similar Threads
Nested/Inner classes Example program
more about nested class topics
accessing shadowed final values in method local inner class
Inner Class Doubt
doubt in InnerClass
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:43:20
.