136k views
4 votes
g If a class named Student has a data member named gpa , and one of its member functions has a parameter also named gpa , how can you refer to the data member inside that member function

User Janeh
by
6.2k points

1 Answer

5 votes

Answer:

By using the this keyword

Step-by-step explanation:

The this keyword in java is used for pointing the current object or current variable .The this keyword is removing the ambiguity among the characteristics of the class as well as the variables with the similar name.

In the given question if the class student has member variable gpa also we have a method having the argument gpa with the help of this keyword we can refer the gpa variable inside the method .

Following are the implementation of the given question

public class Main // main class

{

int gpa; // variable declaration

public Main(int gpa) // constructor

{

this.gpa = gpa; // this keyword

}

public static void main(String[] args) // Main method

{

Main m= new Main(55); // creating object of class

System.out.println("The Value of gpa is : " + m.gpa); // display value

}

}

Output:

The Value of gpa is :55

User Yuriy Kharchenko
by
6.1k points