0% found this document useful (0 votes)
18 views20 pages

1-4-3-Static Members

The document discusses the use of static members and access specifiers in Java, explaining the 'this' keyword, static variables, and methods. It highlights the differences between instance and static variables, as well as the visibility control provided by public, private, and protected access modifiers. Additionally, it covers the importance of encapsulation and memory management in Java.

Uploaded by

sakthisharan9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views20 pages

1-4-3-Static Members

The document discusses the use of static members and access specifiers in Java, explaining the 'this' keyword, static variables, and methods. It highlights the differences between instance and static variables, as well as the visibility control provided by public, private, and protected access modifiers. Additionally, it covers the importance of encapsulation and memory management in Java.

Uploaded by

sakthisharan9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Static members and

access specifiers in Java

S.Rajalakshmi
AP / CSE
SSNCE

1
Session Objectives
◼ this keyword
◼ static keyword
◼ Access specifiers

2
this keyword
◼ this keyword can be used to refer to the object
itself.
It is generally used for accessing class members
(from its own methods) when they have the same
name as those passed as arguments.

public class Circle {


public double x,y,r;
// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
} 3
Static Members
◼ Java supports definition of global methods and
variables that can be accessed without creating
objects of a class. Such members are called Static
members.
◼ Define a variable by marking with the static methods.
◼ This feature is useful when we want to create a
variable common to all instances of a class.
◼ One of the most common example is to have a
variable that could keep a count of how many objects
of a class have been created.
◼ Note: Java creates only one copy for a static variable

4
Static Variables
◼ Define using static:

public class Circle {


// class variable, one for the Circle class, how many circles
public static int numCircles;

//instance variables,one for each instance of a Circle


public double x,y,r;
// Constructors...
}

◼ Access with the class name (ClassName.StatVarName):

nCircles = Circle.numCircles;
5
Static Variables - Example
◼ Using static variables:

public class Circle {


// class variable, one for the Circle class, how many circles
private static int numCircles = 0;
private double x,y,r;

// Constructors...
Circle (double x, double y, double r){
this.x = x;
this.y = y;
this.r = r;
numCircles++;
}
}
6
Class Variables - Example
◼ Using static variables:

public class CountCircles {

public static void main(String args[]){


Circle circleA = new Circle( 10, 12, 20); // numCircles = 1
Circle circleB = new Circle( 5, 3, 10); // numCircles = 2
}
}

circleA = new Circle(10, 12, 20) circleB = new Circle(5, 3, 10)

numCircles
7
Instance Vs Static Variables
◼ Instance variables : One copy per object.
Every object has its own instance
variable.
◼ E.g. x, y, r (centre and radius in the circle)

◼ Static variables : One copy per class.


◼ E.g. numCircles (total number of circle
objects created)

8
Static Methods
◼ A class can have methods that are defined as
static (e.g., main method).
◼ Static methods can be accessed without using
objects. Also, there is NO need to create
objects.
◼ They are prefixed with keyword “static”
◼ Static methods are generally used to group
related library functions that don’t depend on
data members of its class. For example, Math
library functions.
◼ Math.pow(3,5); 9
A Program with Method Overloading
// Compare.java: a class comparing different items
class Compare {
static int max(int a, int b)
{
if( a > b)
return a;
else
return b;
}
static String max(String a, String b)
{
if( a.compareTo (b) > 0)
return a;
else
return b;
}

public static void main(String args[])


{
String s1 = "Melbourne";
String s2 = "Sydney";
String s3 = "Adelaide";

int a = 10;
int b = 20;

System.out.println(max(a, b)); // which number is big


System.out.println(max(s1, s2)); // which city is big
System.out.println(max(s1, s3)); // which city is big
}
10
}
Comparator class with Static
methods
// Comparator.java: A class with static data items comparision methods
class Comparator {
public static int max(int a, int b)
{
if( a > b)
return a;
else
return b;
}

public static String max(String a, String b)


{
if( a.compareTo (b) > 0)
return a;
else
return b;
}
} 11
public class MyClass {
public static void main(String args[])
{
String s1 = "Melbourne";
String s2 = "Sydney";
String s3 = "Adelaide"; Directly accessed using ClassName (NO Objects)

int a = 10;
int b = 20;

Comparator c=new Comparator();


System.out.println(Comparator.max(a, b)); // which number is big
System.out.println(Comparator.max(s1, s2)); // which city is big
System.out.println(c.max(s1, s3)); // which city is big
}
}
12
Static methods restrictions
◼ They can only call other static methods
directly.
◼ They can only access static data.
◼ They cannot refer to “this” or “super”
(more later) in anyway.

13
Visibility Control: Data Hiding and
Encapsulation
◼ Java provides control over the visibility of
variables and methods, encapsulation, safely
sealing data within the capsule of the class
◼ Prevents programmers from relying on details
of class implementation, so you can update
without worry
◼ Helps in protecting against accidental or
wrong usage.
◼ Keeps code elegant and clean (easier to
maintain)
14
Visibility Modifiers: Public,
Private, Protected
◼ Public keyword applied to a class, makes it
available/visible everywhere. Applied to a method or
variable, completely visible.
◼ Default (No visibility modifier is specified): it behaves like
public in its package and private in other packages.
◼ Private fields or methods for a class only visible
within that class. Private members are not visible
within subclasses, and are not inherited.
◼ Protected members of a class are visible within the
class, subclasses and also within all classes that are
in the same package as that class.
◼ Default : Package private – members are private to
the package in which it is present

15
Visibility
public class Circle {
private double x,y,r;

// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
16
Visibility
Circle Construction time message

Circle
area

message Center (x,y)


Radius r

circumference

message

17
Accessors
public class Circle {
– “Getters/Setters”
private double x,y,r;

//Methods to return circumference and area


public double getX() { return x;}
public double getY() { return y;}
public double getR() { return r;}
public double setX(double x) { this.x = x;}
public double setY(double y) { this.y = y;}
public double setR(double r) { this.r = r;}

18
Objects Cleanup/Destructor
◼ Unlike c and c++, memory deallocation is automatic
in java, don’t worry about it
◼ no dangling pointers and no memory leak

problem.
◼ Java allows you to define finalizer method, which is
invoked (if defined) just before the object
destruction.
◼ In way, this presents an opportunity to perform
record-maintenance operation or cleanup any special
allocations made by the user.
◼ protected void finalize() { }
19
// done with this circle
protected void finalize() {
Circle.numCircles = Circle.numCircles--;
System.out.println(“number of circles:”+ Circle.num_circles);
}

Runtime.runFinalizersOnExit(true); ----- remove all memory


System.gc(); ----- remove unused memory

20

You might also like