• 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

Hello World

 
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Example3{
int a=10;
Example3(){
a=20;
}
public static void main(String args[]){

Example3 ex3=new Example3();
System.out.println(ex3.a);
}
}
Outputs:20

Whereas the Output for the following code should be

zero

but its otherwise
class Example3{
int a=10;

public static void main(String args[]){
Example3 ex3=new Example3();
System.out.println(ex3.a);
}
}

Outputs:10
Expected:0 why because "in theory" when a object gets created JVM supplies a default constructor and it sets the values to zero's and nulls,Not the values that were loaded in by the class loader. Did i get it wrong or what??
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zoheb hassan wrote:... when a object gets created JVM supplies a default constructor and it sets the values to zero's and nulls...



No. The only thing a default constructor does is to call it's parent's no arg constructor. It doesn't initialise any variables.
If you had not assigned a value to 'a' then it would have had the default value of zero, but you did assign a value and so that is the value it will have.
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You very much for the reply joanne, can you please explain me the answer below then

class Example3{
int a=10;
Example3(){
a=20;
}
public static void main(String args[]){

Example3 ex3=new Example3();
System.out.println(ex3.a);
}
}
Outputs:20
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a new instance of Example3, it first sets 'a' to 10. Then, because you called the default constructor, it sets 'a' to 20, which is why it outputs 20. If the line 'a=20;' were not there, then the output to your program would be '10'.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zoheb,

In your constructor, you change the value of a to 20. That's why you get 20 as output.

John.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, a variable in java will change it's value if you declare it to different value.

int a = 10;

a = 20;

You changed the value of the variable int a. It first represented 10, and then you changed the value to 20. If you want something else to represent 20, besides a, you can give it a different name. For example:

int a = 10;
int b = 20;

or declare it and initialize it in two different steps:

int a, int b;
a = 10;
b = 20 ;

That way it doesn't over ride the value of a.

In your code, you shouldn't declare a again. You should be able to simply use a, which represent 10. You don't have to tell the compiler that a = 10 again once it's been declared.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reply to "Hello World", it says

Hello, Zoheb Hassan.

Please use a thread title which tells us what the thread is about; I can't see anything about Hello World in your thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic