0% found this document useful (0 votes)
45 views18 pages

BITS Pilani: Access Modifiers, Constructors, and Reference Types

This document discusses access modifiers, constructors, and passing arguments by value vs reference in Java. It explains that public variables and methods can be accessed from outside a class, while private variables and methods cannot. Constructors initialize fields when an object is instantiated. Overloaded constructors allow initializing fields with different arguments. Arguments of primitive types are passed by value, while object references are passed by reference, meaning changes to the object are reflected in the calling code.

Uploaded by

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

BITS Pilani: Access Modifiers, Constructors, and Reference Types

This document discusses access modifiers, constructors, and passing arguments by value vs reference in Java. It explains that public variables and methods can be accessed from outside a class, while private variables and methods cannot. Constructors initialize fields when an object is instantiated. Overloaded constructors allow initializing fields with different arguments. Arguments of primitive types are passed by value, while object references are passed by reference, meaning changes to the object are reflected in the calling code.

Uploaded by

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

BITS Pilani

Pilani Campus

Access Modifiers, Constructors, and Reference Types


Access Modifiers – public variables
public class A {
public int i, j;
public void show(){
System.out.println("i = " + i + " and j = " + j );
}
}

public class Test {


public static void main(String[] args) {
A a = new A();
a.i = 5; /* public variables can be accessed
a.j = 10; outside of class definition */
a.show();
}
}

BITS Pilani, Pilani Campus


Access Modifiers – private variables
public class A {
private int i, j;
public void show(){
System.out.println("i = " + i + " and j = " + j );
}
}

public class Test {


public static void main(String[] args) {
A a = new A();
Test.java:11: i has private access in A
a.i = 5;
a.i = 5;
a.j = 10;
^
a.show();
Test.java:12: j has private access in A
}
a.j = 10;
}
^
2 errors

BITS Pilani, Pilani Campus


Access Modifiers – public methods
public class A {
public int i, j;
public void show(){
System.out.println("i = " + i + " and j = " + j );
}
}

public class Test {


public static void main(String[] args) {
A a = new A();
a.i = 5;
a.j = 10; /* public methods of a class can be
a.show(); invoked from outside of the class
} definition */
}

BITS Pilani, Pilani Campus


Access Modifiers – private methods
public class A {
private int i = 5, j = 10;
private void show(){
System.out.println("i = " + i + " and j = " + j );
}
}

public class Test {


public static void main(String[] args) {
A a = new A();
a.show(); /* The method show() from the type A is
} not visible */
}

BITS Pilani, Pilani Campus


Access Modifiers –
public class A {
private int i, j;
private void show(){
System.out.println("i = " + i + " and j = " + j );
}
public void display(){
i = 5;
j = 10;
/* public or private methods of a class
show();
can refer to both public or private fields
}
and methods of the same class*/
}

public class Test {


public static void main(String[] args) {
A a = new A();
a.display ();
}
}

BITS Pilani, Pilani Campus


Constructors –
public class A {
private int i;
public A(){ /* Constructor is called to instantiate
i = 5; a class and initialize the fields */
}
public void show(){
System.out.println(" Value of i = " + i)
}
}

public class Test {


public static void main(String[] args) {
A a = new A();
a.display (); /* A call to the constructor is the
} first thing that happens when you try
} to instantiate any class */

BITS Pilani, Pilani Campus


Overloading Constructors –
public class A {

private int i;

public A(){
i = 5;
}
/* Overloaded constructors are used to
public A(int x){ instantiate and initialize the fields
i = x; with the values supplied via the
} constructor */

public void show(){


System.out.println(" Value of i = " + i)
}
}

BITS Pilani, Pilani Campus


Overloading Constructors (Contd.)
public class Student { public class TestStudent {
public static void main(String[] args){
private int id; Student s1 = new Student();
private String name; Student s2 = new Student(1, "Avi",9.0);
private double cgpa; Student s3 = new Student(2, "Dol",9.0);
}
public Student(){ }
id = 0;
name = null; /* When a class is instantiated the JVM
cgpa = 0.0; first looks for the no argument constructor
} defined for this class, if it is not found
then the default constructor is called. If
public Student(int i, you have defined an overloaded constructor
for this class then the class should always
String nam, be instantiated with all the arguments
double cgp){ specified in their correct order as
id = i; specified in the constructor declaration.
name = nam; This is because, any constructor that you
cgpa = cgp; explicitly define hides the default
} constructor */
}
BITS Pilani, Pilani Campus
Pass by Value
1. Most methods are passed arguments when they are called. An
argument may be a constant or a variable.
 For example, in the expression
Math.sqrt(33), the constant 33 is passed to the sqrt()
method of the Math class. In the expression
Math.sqrt(x), the variable x is passed.

2. This is simple enough, however there is an important but


simple principle at work here. If a variable is passed, the
method receives a copy of the variable's value. The value of
the original variable cannot be changed within the method.
This seems reasonable because the method only has a copy of
the value; it does not have access to the original variable.
This process is called pass by value.

BITS Pilani, Pilani Campus


Pass by Value - Example
public class DemoPassByValue {
public static void main(String[] args) {
int i = 25;
System.out.println(i);
iMethod(i);
System.out.println(i);
}

public static void iMethod(int iTest) {


iTest = 9; // change it
System.out.println(iTest);
}
}

BITS Pilani, Pilani Campus


Pass by Reference
1. See Java Memory Management Slides before reading this section.
2. If the variable passed as an object, then the effect is
different.
3. We often say things like,
 This method returns an object
 This method is passed an object as an argument
4. This not quite true, more precisely, we should say,
 This method returns a reference to an object
 This method is passed a reference to an object as an
argument
5. In fact, objects, per se, are never passed to methods or returned
by methods. It is always "a reference to an object" that is
passed or returned.
6. The term "variable" also deserves clarification. There are two
types of variables in Java:
 Those that hold the value of a primitive data type and those
that hold a reference to an object.
 A variable that holds a reference to an object is an "object
variable" — the prefix "object" is often dropped for brevity.

BITS Pilani, Pilani Campus


Pass by Reference vs Pass by Value
1. Pass by value refers to passing a constant or a variable
holding a primitive data type to a method, and pass by
reference refers to passing an object variable to a method.
2. In both cases a copy of the variable is passed to the
method. It is a copy of the "value" for a primitive data
type variable; it is a copy of the "reference" for an object
variable. So, a method receiving an object variable as an
argument receives a copy of the reference to the original
object.
3. Here's the catch: If the method uses that reference to make
changes to the object, then the original object is changed.
This is reasonable because both the original reference and
the copy of the reference "refer to" to same thing — the
original object.
4. There is one exception: Strings. Since String objects are
immutable in Java, a method that is passed a reference to a
String object cannot change the original object.

BITS Pilani, Pilani Campus


Pass by Reference – Example (StringBuffer)

public class DemoPassByReference1 {

public static void main(String[] args) {


StringBuffer sb = new StringBuffer("Hello, world");
System.out.println(sb);
sbMethod(sb);
System.out.println(sb);
}

public static void sbMethod(StringBuffer sbTest) {


sbTest = sbTest.insert(7, "Java ");
System.out.println(sbTest);
}
}

BITS Pilani, Pilani Campus


Pass by Reference – Example (StringBuffer)

BITS Pilani, Pilani Campus


Pass by Reference – Example (String)

public class DemoPassByReference2 {

public static void main(String[] args) {


String s = "Java is fun!";
System.out.println(s);
sMethod(s);
System.out.println(s);
}

public static void sMethod(String sTest){


sTest = sTest.substring(8, 11);
System.out.println(sTest);
}
}

BITS Pilani, Pilani Campus


Pass by Reference – Example (String)

BITS Pilani, Pilani Campus


THANK YOU

BITS Pilani, Pilani Campus

You might also like