Posts

Showing posts with the label static

Java 8 Static Interface methods with code samples

Just like default methods , Java 8 also supports static methods in interfaces. In this blog post, I will be explaining Java 8 static interface methods in detail. I will be covering what static methods are, how they work and why they were added. Edit What are static interface methods? Static interface methods are methods with the static keyword are with a method body. How do static interface methods work? Consider the following code snippet: </pre>public interface MyInterface {static public void staticMethod(){System.out.println("In staticMethod of interface");}}<pre> So in the code above, the staticMethod is a static interface method.  So we have defined a method with a method body within the interface. It has the static keyword specified. Prior to Java 8, the above code would have caused an error due to two reasons: There is a method with a method body within the interface There is a static method with in the interface How are they similar to default method...

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...

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...