Java Access Modifiers, This & Static Keywords
Java Access Modifiers, This & Static Keywords
Task 1: Write another class in which you write main method and access the
attributes of following class.
Task 2: Write constructor of the above class (Test.java) and declare it
private. Now try to create object of the above class in another class.
Solution:
Task 3: Run following code. Is it written correctly?
this Keyword
There can be a lot of usage of java this keyword. In java, this is a reference variable that
refers to the current object.
o refer current class instance variable.
o invoke current class method (implicitly)
o invoke current class constructor.
o can be passed as an argument in the method call.
o can be passed as argument in the constructor call.
o can be used to return the current class instance from the method.
Sample code
class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x);
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}
OUTPUT:
hello a
10
Task 4: this: to refer current class instance variable. Write a class in which
names of class data members are same as constructor arguments.
Definition of the constructor should be similar to following
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
Display rollno, name and fee values in display method
Now create two objects of this class and display their value
Observe output and identify problem
Now rewrite constructor function as follows
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
again run the code and see output
Solution:
The this() constructor call can be used to invoke the current class constructor. It is used to
reuse the constructor. In other words, it is used for constructor chaining.
2. create a constructor of this class which takes rollno, course and name as parameter
and assign them to variables of rollno, course and name respectively
3. Create another constructor which takes arguments: rollno, course, name and fee.
Assign fee to fee variable and call first constructor to assign other three arguments.
static Keyword
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
Static method can access static data member and can change the value of it.
If you declare any variable as static, it is known static variable.
The static variable can be used to refer the common property of all objects (that is not unique
for each object) e.g. company name of employees, college name of students etc.
The static variable gets memory only once in class area at the time of class loading.
Task 6:
A. Calculate cube using a method
Create a class named as calculate
Write a method, count in which returns cube of the given variable.
Write main method which calls the count method
B. Calculate cube using static method
Now change count method to static by adding static keyword
Call count method from main to calculate cube
QUESTIONS
Please Fill the blank space with respective answers to following questions:
Yes, a constructor of a class can be declared private in Java. When a constructor is private, it
restricts the creation of objects outside the class. This is useful for scenarios like
implementing the Singleton pattern, controlling object creation, and preventing instantiation
of utility classes. It also limits subclassing from outside the class and allows controlled
initialization procedures.
Private data members are accessible only within the class they are
declared in.
Protected data members are accessible within the same package
and by subclasses, regardless of package.
Private members offer stricter encapsulation, limiting access to just
the class itself, while protected members provide a balance
between encapsulation and extensibility, allowing subclasses to
access them.
Question 3: What is ‘default’ access modifier?
the 'default' access modifier in Java allows access within the same package but
restricts access from classes in other packages. It's used when no access
modifier is explicitly specified and is the default access level applied to
members if none is specified.
class ClassConstructorOperations
{
public static void main(String s[])
{
Student student = new Student( "Lavanya", 501, 95.6 );
System.out.println(student.rollNumber + " is the roll number of " + student.name + "
and she secured " + student.percentage + " % in SSC" );
}
}
class Student
{
String name;
int rollNumber;
double percentage;
//Write a constructor here to initialize the parameters.
}
class Student {
String name;
int rollNumber;
double percentage;