• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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.
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic