Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

C++ Classes & Data Abstraction
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Static Poisoning. Review: class and instance variables int is a data type; 3 is a value (or instance) of that type A class is a data type; an object is.
Classes and Objects in Java
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Using Statics. 2 Review: class and instance variables int is a data type; 3 is a value (or instance) of that type A class is a data type; an object is.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
UML Class Diagram: class Rectangle
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Strings, StringBuilder, StringBuffer. String Strings in java are immutable Once created they cannot be altered and hence any alterations will lead to.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
Multithreading.
Saravanan.G.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
ECE122 Feb. 22, Any question on Vehicle sample code?
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
Java Programming static keyword.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Nested Classes CompSci 230 S Software Construction.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSC 212 – Data Structures Lecture 6: Static and non-static members.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Static Members Static Variables & Methods SoftUni Team Technical Trainers Software University
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
using System; namespace Demo01 { class Program
Classes and Objects in Java
Review Session.
UML Class Diagram: class Rectangle
Using local variable without initialization is an error.
Encapsulation.
CSC 113 Tutorial QUIZ I.
The Boolean (logical) data type boolean
Polymorphism CT1513.
Classes & Objects: Examples
METHOD OVERRIDING in JAVA
S.VIGNESH Assistant Professor, SECE
Classes and Objects in Java
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Object Oriented Programming (OOP) Lecture No. 11
Unit-2 Objects and Classes
Classes and Objects in Java
CSG2H3 Object Oriented Programming
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Static Keyword

What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All static variables and methods belong to the class and not to any instance of the class

When can we access static variable When a class is loaded by the virtual machine all the static variables and methods are available for use. Hence we don’t need to create any instance of the class for using the static variables or methods. Variables which don’t have static keyword in the definition are implicitly non static.

Example Class staticDemo{ public static int a = 100; // All instances of staticDemo have this variable as a common ` variable public int b =2 ; public static showA(){ System.out.println(“A is “+a); } Class execClass{ public static void main(String args[]){ staticDemo.a = 35; // when we use the class name, the class is loaded, direct access to a without any instance staticDemo.b=22; // ERROR this is not valid for non static variable staticDemo demo = new staticDemo(); demo.b = 200; // valid to set a value for a non static variable after creating an instance. staticDemo.showA(); //prints 35 }

Static and Non-static We can access static variables without creating an instance of the class As they are already available at class loading time, we can use them in any of our non static methods. We cannot use non static methods and variables without creating an instance of the class as they are bound to the instance of the class. They are initialized by the constructor when we create the object using new operator.

How it works Basic Steps of how objects are created 1.Class is loaded by JVM 2.Static variable and methods are loaded and initialized and available for use 3.Constructor is called to instantiate the non static variables 4.Non static variables and methods are now available As all the non static variable are available only after the constructor is called, there is a restriction on using non static variable in static methods.

Why do we need this Static methods are identified to be mostly used when we are writing any utility methods. We can also use static variables when sharing data. When sharing data do keep in mind about multithreading can cause inconsistency in the value. (synchronize the variable)

Further Reading vaOO/classvars.html