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

Answers were Sorted based on User's Feedback



why top level class could not be static..

Answer / vatsal doshi

The answer is in the question itself.
What is static? Something which belongs to a class and not its objects.

So if in a class, we have some variables, having single copy, we call them static.
Similarly some methods may be actually manipulating these static variables, so those methods are also static.

However, the class itself is static only if it belongs to some class(Definition of static)

So, for a class to be static, it must be a nested class. Such nested classes are called as Top Level Nested Classes in Java.

Is This Answer Correct ?    7 Yes 1 No

why top level class could not be static..

Answer / sanket mehta

static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class

If you declare the outer class as static, it will not allow to compile giving : Illegal modifier for the class classname; only public, abstract & final are permitted

Is This Answer Correct ?    0 Yes 0 No

why top level class could not be static..

Answer / 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

More Core Java Interview Questions

What are advantages of using Java?s layout managers than windowing systems?

0 Answers  


What Is Resource Leak?

2 Answers  


Is {a, n, d} a palindrome? If you are given a random string, is it a palindrome or not?

0 Answers   Amazon,


What do you mean by append?

0 Answers  


why java does not support mulitple inheritance directly?

3 Answers   TCS,


What is string args [] in java?

0 Answers  


Why const and goto are reserved keyword in java?

1 Answers  


Explain the difference between runnable and callable interface in java?

0 Answers  


What is the difference between an object-oriented programming language and object-based programming language?

0 Answers  


10. class Nav{ 11. public enum Direction { NORTH, SOUTH, EAST, WEST } 12. } 13. public class Sprite{ 14. // insert code here 15. } Which code, inserted at line 14, allows the Sprite class to compile? a)Direction d = NORTH; b)Nav.Direction d = NORTH; c)Direction d = Direction.NORTH; d)Nav.Direction d = Nav.Direction.NORTH;

2 Answers  


What is the static import?

0 Answers  


What is extension method in java?

0 Answers  


Categories