• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

why in x=x++ x keep not changed?  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't under in x=x++ x keep not changed?
and another question:
class Process {
byte b=127;
Process() {
this.methodA();
}

void methodA() {
System.out.println("Value of b is = " + b );
}

public static void main(String [] args) {
Processor p = new Processor();
}
}

class Processor extends Process {
byte b=126;

Processor() {
System.out.println("Value of b = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
What is the Output?
1.Prints Value of b = 0 and Value of b is = 126.
2.Compile-time error occurs.
3.Prints Value of b = 126 and Value of b = 126.
4.Prints Value of b = 127 and Value of b = 126.
why answer is 1--Prints Value of b = 0 and Value of b is = 126.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have added line numbers to explain the exeution sequence.
When you create a new Processor(), the super() is invoked, from Process, when you invoke this.method it will go to Processor method, at that time the instance of the processor is not initialized, so the default value of b =0 is printed. Then the remaining part of super() is exeuted.(i.e SOP).
Next, the instance variables are intialiazed in Processor.(b = 126), then the SOP is printed b = 126.


class Process {
byte b=127;
Process() { //(2)
this.methodA();
System.out.println("Constructor in Super");
}
void methodA() {
System.out.println("Value of b is = " + b );
System.out.println("superMethod");
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() { //(1)
System.out.println("Sub constructor");
System.out.println("Value of b = " + b); //(4)
}
void methodA() { //(3)
System.out.println("Sub method");
System.out.println("Value of b = " + this.b);
}
}


Refer JLS chapter 12.5
http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670
Hope this helps,
Vanitha.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for your first question refer this discussion http://www.javaranch.com/ubb/Forum24/HTML/009768.html
 
jack zhu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much,Vanitha.
But I still have a question:
The statement x=x++ ,why x keep no change?
 
jack zhu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Anshul Manisha! I have learn more.Thanks again!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x=x++; ???
when u execute this statement u'll find no change in the value of (x), this is bcuz in this statement it will first execute (x=x) and then (x++). but there will be no change in the value of (x) bcuz final value of x is (x=x) that was assigned b4 (x++).
clear??? or need more explanation.......
===
FAF
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!