• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variables passing to method

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class D {
private boolean b1, b2;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void m1 () {
System.out.println(b1+","+b2);
public static void main (String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}
I Expected the ans should false,false.
But how it is true,true.
Here b1,b2 are the private variables which can be avilable to method m1().
By default these variable values are false,false.Eventhough we are passing the values.
Please Explain.Sometimes Iam confusing.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sukhavasi!

First of all we have to solve a little problem with your code: before the main()-method a "}" is missing and after it there is one "}" too much.

The output true, true is definitely what we should expect :-)

1. When you instantiate class D both instance variables b1 and b2 are set to false (default value for the primitive data type boolean).
2. The method call d.setB1(true); passes the value true to the setB1()-method and it assigns true to the instance-variable b1.
(we don't pass a reference to the method!)
Note: you also could write
public void setB1(boolean b) {
this.b1 = b;
}
3. The same happens with b2

Bye,
Robert
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the answer "true true" is correct.even though the variables are private they are all accessible in
the methods of the same class.
here setB1 and setB2 both belong to class D.
look carefully you are calling the setter method first and then m1(). once you created the object, both b1 & b2 were given false value. but you changed those values in the setter methods and then you are displaying them.
but if you display first and then call the setter methods you will get "false false".
change your code as follows and observe the behaviour::

now you will get "false false".
[ July 28, 2007: Message edited by: Priyam Srivastava ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sukhavasi vasavi:
I Expected the ans should false,false.
But how it is true,true.

You explicitly set the values.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic