Posts

Showing posts with the label Java Keywords

public static void main explained

I’ve seen a lot of questions posted which ask the meaning of public static void main in Java. In this blog post, I’m going to explain these keywords which are used with the main method.   The main method is the starting point in any standalone Java application. However, whenever you write a main method in Java, you need to precede it with the public static void keywords. So why are they required?   Public –  Public is an access specifier. Refer this post to learn about the other access specifiers in Java.  When the public access specifier is used for a member, it indicates that the member is accessible by code which is outside the class.  So the main method needs to be public since it has to be accessible by the JVM when the program is run.   Static – The static keyword indicates that a member can be accessed  without requiring an object of a class. Refer this post for more information on the static keyword. So they keyword static allowes the m...

Java This Keyword Explained

In this blog post, I will be explaining the ‘this’ keyword. Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object i.e. the object on which the method was invoked. Consider the following code snippet: private int radius;public  Circle(int radius){this.radius = radius;}   Since both the passed in variables and the instance fields have the same names, this is used to distinguish. So this.radius refers to the current object’s copy of the radius instance field & radius refers to the value passed in.

Static keyword explained

In this blog post, I will be explaining Java’s static keyword. What is the static keyword? There will be times when you will want to define a class member i.e. either method or field independent of any object of that class. To create such a member, we need to precede its declaration with the   static keyword .  When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object, i.e. it can be accessed directly with the class name. You can declare both methods and variables to be static.   What is a Static Variable? A variable preceded with the static keyword is a static variable.  It can be accessed irrespective of any member directly using the class name. Variables declared as static are essentially, global variables. When objects of a class are created, no copy of a static variable is made. Instead, all objects of the class share the same static variable.   Static method A method preceded with the stati...

What is the final keyword in Java?

A very common interview question is to ask about the final keyword and what it means when it is used with different constructs like field, method, etc. So in this blog post I’m going to explain the final keyword in detail. Final Variable A variable can have the  final keyword specified.   Final keyword for a variable means that you cannot modify the contents of the variable after you declare it. So you cannot assign a value to a final variable, doing so will result in a compilation error. So you must initialize a final variable when you declare it. A final variable is generally used to define a constant in Java. It is a common coding convention to choose all uppercase identifiers for final variables. Final Method A method can have the final keyword specified. Final keyword for a method means that you cannot override the method. So if a sub-class tries to override a final method,  there will be a compilation error.  Although you cannot override a final method, a subclass still i...