SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Static DhrubojyotiKayal
Need for static members static fields Static methods Agenda
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.  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.  Need for static members
When you say something is static, it means that particular field or method is not tied to any particular object instance of that class  So even if you’ve never created an object of that class you can call a static method or access a static field.  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. 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.  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  static
class StaticTest {  	static inti = 47;  }  StaticTest st1 = new StaticTest();  StaticTest st2 = new StaticTest();  Both st1.i and st2.i have the same value of 47 since they refer to the same piece of memory. Accessing static fields  Use reference Use class name – StaticTest.i Static fields
class Incrementable {  	static void increment() { StaticTest.i++; }  }  Incrementablesf = new Incrementable();  sf.increment();  Static method
Create a Java class with the static main method Declare a static integer field and set it to 30 Create a static method which increments this field and prints it Create a non static method which prints the value in the static integer field Try to remove the print in the static method and call the non static method instead Exercise
Q&A

Weitere ähnliche Inhalte

Ähnlich wie 11 static

Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic ConceptsVicter Paul
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
3.Classes&Objects.pptx
3.Classes&Objects.pptx3.Classes&Objects.pptx
3.Classes&Objects.pptxPRABHUSOLOMON1
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsHelen SagayaRaj
 
Check the true statement(s) (there is at least one correct choice, p
 Check the true statement(s)  (there is at least one correct choice, p Check the true statement(s)  (there is at least one correct choice, p
Check the true statement(s) (there is at least one correct choice, plicservernoida
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08Terry Yoast
 
static method,adding method dynamically.pptx
static method,adding method dynamically.pptxstatic method,adding method dynamically.pptx
static method,adding method dynamically.pptxPooraniBalamurugan3
 

Ähnlich wie 11 static (20)

Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Static variable
Static  variableStatic  variable
Static variable
 
Hemajava
HemajavaHemajava
Hemajava
 
This and Static Keyword
This and Static KeywordThis and Static Keyword
This and Static Keyword
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
3.Classes&Objects.pptx
3.Classes&Objects.pptx3.Classes&Objects.pptx
3.Classes&Objects.pptx
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Java basics
Java basicsJava basics
Java basics
 
Check the true statement(s) (there is at least one correct choice, p
 Check the true statement(s)  (there is at least one correct choice, p Check the true statement(s)  (there is at least one correct choice, p
Check the true statement(s) (there is at least one correct choice, p
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
static method,adding method dynamically.pptx
static method,adding method dynamically.pptxstatic method,adding method dynamically.pptx
static method,adding method dynamically.pptx
 

Mehr von dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requestsdhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servletsdhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variablesdhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with javadhrubo kayal
 

Mehr von dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

11 static

  • 2. Need for static members static fields Static methods Agenda
  • 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. 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. Need for static members
  • 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 So even if you’ve never created an object of that class you can call a static method or access a static field. 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. 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. 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 static
  • 5. class StaticTest { static inti = 47; } StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest(); Both st1.i and st2.i have the same value of 47 since they refer to the same piece of memory. Accessing static fields Use reference Use class name – StaticTest.i Static fields
  • 6. class Incrementable { static void increment() { StaticTest.i++; } } Incrementablesf = new Incrementable(); sf.increment(); Static method
  • 7. Create a Java class with the static main method Declare a static integer field and set it to 30 Create a static method which increments this field and prints it Create a non static method which prints the value in the static integer field Try to remove the print in the static method and call the non static method instead Exercise
  • 8. Q&A