Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Similar presentations


Presentation on theme: "ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya."— Presentation transcript:

1 JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya

2 Introduction How and where do we use static keyword To variables To methods Advantages of using Static keyword Summary

3  Static is a keyword that states that all instances of a given class are to share the same variable or method.  This is used for a constant variable or a method that is the same for every instance of a class  This presentation will provide information about uses of Static keyword in JAVA

4  Without the “static” keyword, it's called “instance variable”, and each instance of the class has its own copy of the variable.  When a variable is declared with the static keyword, its called a “class variable”. All instances share the same copy of the variable

5  A common use of static variables is to define "constants“.  A class variable can be accessed directly with the class, without the need to create a instance.  Variables can be declared with the “static” keyword like this. Example: static int y=0;

6  Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”.  Methods declared with static cannot access variables declared without static. public class Main { static int xl = 0; int x = 20; public static void m() { x = 150; //non static x cannot be access } public static void main(String[] args) { m(); }

7 public class Main { public int m (){ int x = 20; return x; } public static void main(String[] args){ Main ws = new Main(); ws.m(); } The following gives a compilation error, unless m method is also static. Or unless it is accessing through object public class Main { public int m (){ int x = 20; return x; } public static void main(String[] args) { m (); } A Note: There's no such thing as static classses. “static” in front of class creates compilation error.

8 ***Remember that static methods can’t be OVERRIDDEN. But that doesn’t mean that they cant be redefined in a sub class. class Main { static void travel() { System.out.print("A"); } class main extends subMain { static void travel() { System.out.print("B"); //this is redefinition NOT overridden }

9 Static variables can be use to hold constants (values that are not changing). Static method’s behavior has NO dependency on the state of an object. That mean methods which are declared as static will always runs the same way.

10 Documentation. Anyone seeing that a method is static will know how to call it. Any programmer looking at the code will know that a static method can't interact with instance variables, which makes reading and debugging easier. Efficiency. A compiler will usually produce slightly more efficient code because no implicit object parameter has to be passed to the method.

11 Summary Q & A

12 Thank You


Download ppt "ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya."

Similar presentations


Ads by Google