• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

what is the meaning of this java code

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following java code segments,

class Test{
int a=100;
private int b=200;

public void print(){
System.out.println(a);
System.out.println(b);
}
public void setB(int b){this.b=b;} //what is this
public int getB(){return b;} // and this
}

class Demo{
public static void main(String args[]){
Test t1=new Test();
System.out.println(t1.a);
System.out.println(t1.getB());
t1.setB(1000);
System.out.println(t1.getB());
}
}

I have comment above code. how we can use this in java code
 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

refers to the instance of the current object.

when you have the setter method as below

You are seting the value that is passed as the paramete to the instance variable b

The getter method below is simply returning the value of the instance variable.

 
Bartender
Posts: 383
47
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch! You can format your code by clicking the button that says "code" in the post editor, which will give you two tags: [code] and [/code]. Your code needs to go between these tags. There, that looks better  

 
Marshal
Posts: 75839
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code shows that fields not marked private are accessible from outwith their class. The field a can be changed from elsewhere, so it shows lack of encapsulation.
You have been told correctly what the setB() method does, but it hasn't been explained. Because you have a field and a parameter both with the same name, the parameter takes precedence. To make sure you are referring to the field rather than the parameter, you call the field this.b. As you know, this is a keyword meaning the current object.

And (again) welcome to the Ranch
 
reply
    Bookmark Topic Watch Topic
  • New Topic