javatutelearn.com
Easy Learning
Search Javatutelearn.com :
class student { // followings are instance variables String name; int rollno; String address; // In the following method, names of local variables (i.e. arguments in the method) are same as names of instance variables // In this case, when we try to access variables, then local variables will be accessed, instance variables can not be accessed. // Because local variables hide instance variables of the same name. public void setdata (String name, int rollno, String address) { } }
class student { // followings are instance variables String name; int rollno; String address; public void setdata (String name, int rollno, String address) { // following is use of this keyword this.name = name; this.rollno = rollno; this.address = address; } }
public class student { // followings are instance variables String name; int rollno; String address; public void setdata (String name, int rollno, String address) { // following is use of this keyword this.name = name; this.rollno = rollno; this.address = address; } public void dispdata(){ // here, no need to use this keyword // because same name local variables are not present System.out.println("Name is " + name); System.out.println("Rollno is "+ rollno); System.out.println("Address is "+address); } public static void main(String args[]) { student s1 = new student(); s1.setdata("Gargi",170067,"New Delhi"); s1.dispdata(); } }
Name is Gargi Rollno is 170067 Address is New Delhi