Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

why top level class could not be static

Answer Posted / sathishkumarbabu

All top-level classes are, by definition, static.

What the static boils down to is that an instance of the class can stand on its own. Or, the other way around: a non-static inner class (= instance inner class) cannot exist without an instance of the outer class. Since a top-level class does not have an outer class, it can't be anything but static.

Because all top-level classes are static, having the static keyword in a top-level class definition is pointless.

Some code to play around with:

public class Foo {

public class Bar {
// Non-static innner class
}

public static class Baz {
// Static inner class
}
}

public class Example {
public static void main(String[] args) {
new Foo(); // this is ok
new Foo.Baz(); // this is ok
new Foo.Bar(); // does not compile!

Foo f = new Foo();
Foo.Bar bar = f.new Bar(); //this works, but don't do this
}
}

I put the "but don't do this" in there because it's really ugly code design. Instance inner classes should not be visible outside the outer class. They should only be used from within the outer class.

Regards : Barend Garvelink

Is This Answer Correct ?    3 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is meant by binding in rmi?

957


How to sort array of 0 and 1 in java?

898


Differentiate between postfix and prefix operators in java.

1082


What is constant in programming?

1030


What does java edition mean?

1040


Can you sort a list in java?

927


Give an example of call be reference significance.

986


Explain how hashmap works?

1007


Howto get an object that will perform date & time calculations then format it for output in some different locales with different date style.can ne1 tel me the answer of this question.pls

1852


How will you reverse a link list without using recursion?

988


Is set ordered?

923


How do you do descending order in java?

911


What is the hashcode () and equals () used for?

957


What lambda means?

939


Does java support Operator Overloading?

990