Java Static Methods.
Java Static Methods
-
Static methods (class methods), like static variables, belong to the class.
(not the object of a class) - They can be invoked by name, through the class name, without any existing object of that class.
- Static methods have only access to other static members of the class.
- It is normal to refer to static type through the class name.
- An interface can contain static constants and methods, which can be referred to directly
through the interface name.
Here is an example:
interface CommonData { static String[] priceCat = {"Low", "Middle", "High"}; static int number = 23; static String str = "Demo sting in Java"; } public class StaticMethods { static void listData() { System.out.println("Value of num=" + CommonData.number); System.out.println("Value of str=" + CommonData.str); for (String cat : CommonData.priceCat) { System.out.println("Category=" + cat); } System.out.println("Last Category=" + CommonData.priceCat[CommonData.priceCat.length - 1]); } public static void main(String args[]) { StaticMethods.listData(); } }
The result will bee:Value of num=23 Value of str=Demo sting in Java Category=Low Category=Middle Category=High Last Category=High
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.