
Understanding Static Variables and Methods in Java
Learn about static variables and methods in Java, how they are shared among objects and classes, and when to declare a field as static. Examples and explanations provided for better comprehension.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
What is static? CS340100, NTHU Yoshi
Static? ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX = var2; } } Instance members belong to the individual objects Class members belong to the class, shared by all the objects
When we creates many objects Test obj1 = new Test(1,2); Test obj2 = new Test(3,4); Test obj3 = new Test(5,6); Objects (obj1, obj2, obj3) have their own instanceX System.out.println(obj1.instanceX); //prints 2 System.out.println(obj2.instanceX); //prints 4 System.out.println(obj3.instanceX); //prints 6
Static Variable are Shared Static variables do not belong to an individual object In the code above, the value of staticX are modified to 1, 3, 5, sequentially They were modifying the same one (they?) Static variables belong to the class this.staticX = 3 is equivalent to Test.staticX = 3
When will you declare a field as static? Let s see an example You are creating a class for representing the accounts of a bank Each account has the user name, the saving, and the interest rate Each user has different name Each user has different saving The interest rate are identical, shared by all the accounts
Example: Account.java class Account { int money; String name; static double rate = 0.02; //2% public Account(String name, int money) { this.name = name; this.money = money; } }
Example2: class Math { public static final double PI = 3.1415926; } Math.PI
Method ? class Account { private int money; private String name; private static double rate = 0.002; //0.2% public Account(String name, int money) { this.name = name; this.money = money; } public static double getRate() { return rate; } } instance Account
Static method System.out.println( Account.getRate() ); // 0.002 reference variable Account acc1 = new Account( John , 1000); System.out.println( acc1.getRate() ); compile Eclipse Account.getRate()
So now lets check Hello World public class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } } Now you know that why the main method is declared as static?
Overriding in Static Method? No overriding in static method It is known as hiding Which is executed is according to the type
Discussion compile compiler static method non-static variables? Static method main static? static new
Advance ClassLoader? class java.lang.Class ( ) java.lang.Class Class klass = Class.forName( java.lang.String ); klass.newInstance(); Why?
Memory An instance of class XXX Class XXX { Static fields Static methods } New/instantiate XXX.class An instance of class XXX An instance of class XXX
References http://java.sun.com/j2se/1.4.2/docs/api/java/ lang/Class.html