This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

variables passing to method

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic