Static<br />DhrubojyotiKayal<br />
Need for static members<br />static fields<br />Static methods<br />Agenda<br />
You want to have only a single piece of storage for a particular field, regardless of how many objects of that class are c...
When you say something is static, it means that particular field or method is not tied to any particular object instance o...
class StaticTest { <br />	static inti = 47; <br />} <br />StaticTest st1 = new StaticTest(); <br />StaticTest st2 = new St...
class Incrementable { <br />	static void increment() { StaticTest.i++; } <br />} <br />Incrementablesf = new Incrementable...
Create a Java class with the static main method<br />Declare a static integer field and set it to 30<br />Create a static ...
Q&A<br />
Upcoming SlideShare
Loading in...5
×

11 static

276

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
276
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

11 static

  1. 1. Static<br />DhrubojyotiKayal<br />
  2. 2. Need for static members<br />static fields<br />Static methods<br />Agenda<br />
  3. 3. You want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created. <br />You need a method that isn’t associated with any particular object of this class. That is, you need a method that you can call even if no objects are created. <br />Need for static members<br />
  4. 4. When you say something is static, it means that particular field or method is not tied to any particular object instance of that class <br />So even if you’ve never created an object of that class you can call a static method or access a static field. <br />With non-static fields and methods, you must create an object and use that object to access the field or method, since non-static fields and methods must know the particular object they are working with.<br />Java guys use the terms class data and class methods, meaning that the data and methods exist only for the class as a whole, and not for any particular objects of the class. <br />Since static methods don’t need any objects to be created before they are used, they cannot directly access non-static members or methods by simply calling those other members without referring to a named object (since non-static members and methods must be tied to a particular object <br />static<br />
  5. 5. class StaticTest { <br /> static inti = 47; <br />} <br />StaticTest st1 = new StaticTest(); <br />StaticTest st2 = new StaticTest(); <br />Both st1.i and st2.i have the same value of 47 since they refer to the same piece of memory.<br />Accessing static fields <br />Use reference<br />Use class name – StaticTest.i<br />Static fields<br />
  6. 6. class Incrementable { <br /> static void increment() { StaticTest.i++; } <br />} <br />Incrementablesf = new Incrementable(); <br />sf.increment(); <br />Static method<br />
  7. 7. Create a Java class with the static main method<br />Declare a static integer field and set it to 30<br />Create a static method which increments this field and prints it<br />Create a non static method which prints the value in the static integer field<br />Try to remove the print in the static method and call the non static method instead<br />Exercise<br />
  8. 8. Q&A<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×