0% found this document useful (0 votes)
28 views13 pages

Java Access Modifiers, This & Static Keywords

Uploaded by

muhammad shahbaz
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)
28 views13 pages

Java Access Modifiers, This & Static Keywords

Uploaded by

muhammad shahbaz
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/ 13

JAVA ACCESS MODIFIERS, THIS

& STATIC KEYWORDS


Lab-07

Submitted by: Syed Muhammad Ali


( INFT231102007)
Ali Haider ( INFT231102013)
Ather Nazeer ( INFT231102027)
Ateeb Nazeer ( INFT231102028)
Hussain Ali Gill ( INFT231102044)
Daniyal Shahzad ( INFT231102058)
GROUP NO: 06
LAB MANUAL: 07

LAB 07 Access Modifiers, this keyword & static keyword


Lab Objectives:
1. Understanding the use of this keyword
2. Understanding access modifiers
3. Understanding and implementing static keyword concept
Software Required:
Notepad ++ or Textpad
Access Modifiers
Access levels and their accessibility can be summarized as:

Private: Same class.

Protected: Same class, Same package, Subclasses.

Public: Same class, Same package, Subclasses, Everyone.

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

program in which this keyword is not required

Solution:

Task 5: this: to invoke current class constructor

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.

Reuse of constructor from constructor

1. Create a class names as Student

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.

4. Call class using three and four parameters.


5. Solution:

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

Task 7: Implement following class


Now change the count variable to static then observe output.
Output:
Cube of 5 is :125

QUESTIONS

Please Fill the blank space with respective answers to following questions:

Question 1: Can the constructor of a class be declared private? What will


be consequences?

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.

Question 2: What is the difference between private and protected data


members of a class?

 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.

Question 4: Write the content of the constructor whose parameter names

are same as the member variables name.

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.
}

To initialize the parameters in the constructor of the Student class, you


can use the same parameter names as the member variable names.
Here's how you can write the constructor:
java

class Student {
String name;
int rollNumber;
double percentage;

// Constructor with parameters having same names as member variables


public Student(String name, int rollNumber, double percentage) {
this.name = name;
this.rollNumber = rollNumber;
this.percentage = percentage;
}
}
Question 5: What will be the output of the program?
public class Karbon {
static int i = 5;
public static void main(String[] args) {
Karbon test = null;
System.out.print("i = " + test.i + ", ");
System.out.print("i = " + Karbon.i);
}}
Solution:
Exception in thread "main" java.lang.NullPointerException
THE END

You might also like