• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

explain this

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

Why here it is printing 0,1.0 why not 1,1.0 ?
please explain
Everybody in this forum is helping me so I would like thanks to everybody .
payal
[This message has been edited by payal sharma (edited July 27, 2001).]
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Payal,

Hope this helps.
Vanitha.

----------
I edited the comment to multiple lines inside the code block, this was causing the page to run off the edge.--Carl
[This message has been edited by Carl Trusiak (edited July 27, 2001).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure.
You need to know post-fix and pre-fix operators.
So to walk throgh the flow of your program, it starts out by calling the no-args constructor. All this do is call the two args constructor (are you sure that this call souldn't be this(0,0.0)? ).
In thgis constructor, a and y will both be 0. A call is then made to the one-arg constructor. That constructor doesn't do anything, so we return to the two-args constructor, where something gets printed.
When ++ is after the variable, the meaning of it is: "Get the value, then increment". When it is before, it means: "Increment and thenm get the value".
Thus we take the value of x (which is 0) and print that. Then we print a comma, and continues with incrementing y (to 1.0) and printing that.
Does this explain your doubts?
/Mike
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can now understand the postfix and prefix but If ealier is was local variable in the constructor then what is x here now and why its value is update every where

C:\JAVA>java Test
8
1
1
please explain
[This message has been edited by payal sharma (edited July 27, 2001).]
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, lets go through the flow of your program here too:
You start out by creating a new instance of Test. Remember that constructors are runned in order from superclass to subclass.
This means that the method declared as "public Process()" is the one to be runned. The other Process-methods have returntypes, so they are not constructors.
Here the only x known to Process (the one declared as x=y-1 -> x=32) is used. That gets right-shifted twice before printed (thus making it an 8).
The program then continues with the constructors for Test.
Here it uses the overridden version of x, wich is initiated to 0. Since it is a class-variable and not one declared in a method, the ++ done on it in Test(int a) will "keep". This number is then printed twice.
Ok?
/Mike
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh oh, didn't type fast enough - please allow me to ask a question on the previous code and comments:

Originally posted by Vanitha Sugumaran:
class Test {
static int x;
// other code here
so it prints the valur of x (has default value 0).

public static void main(String [] args) {
new Test();
}
}


I just read that the compiler complains about local variables that are not initialized. So here, because it's a static variable, it gets a default value of 0? Is this true of all static variables?
Thanks,
Pauline


[This message has been edited by Pauline McNamara (edited July 27, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In
Test(int a double b){} x is not a local varible.
It is a static variable.
Vanitha
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no I am asking here
Test(int a) {
x++;
System.out.println(x);
}
in second code now
payal
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI payal,
In the second code ,x hides the x in the base class.So it is 1.
Thanx
Rajani

Originally posted by payal sharma:
no I am asking here
Test(int a) {
x++;
System.out.println(x);
}
in second code now
payal


 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all:

Just be careful on local and class instance variables.Reemember that you can hide the instance variables by local variables, which may produce results which you may not expect.
- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
The moustache of a titan! The ad of a flea:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic