Tuesday, May 3, 2011

Some Common Doubts in JAVA

Why is Java called a completely Object Oriented Language?

Unlike other earlier OOP languages like C++, it is impossible to create a program without defining a class in Java. Hence, it is called as completely Objec Oriented Language.

Why is the main() method definition always public static?

To unlock this answer, you must know the use of the keywords 'public' and 'static' in Java.

The 'public' Keyword:

The keyword 'public' is used when we want a method of a class to be accessed by any other class or code in any part of the program. Hence, as we know that main() method should be given open access to the codes in any part of the program, it is given public access.

The 'static' Keyword:

The keyword 'static' can be used before both variable and method instances of a class.

The situations where we need to use a class before defining its objects are the cases where we define a method to be static.

Therefore, it is clear that the main() method should always be declared static as we need to use the main class without creating its object instances.

No comments:

Post a Comment