• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Constructor

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am confused wih the output of this code which is :
Test(1)
Test(3)
Test(2)
I think the code should give compiler error, because there is no zero argument constructor in the Test class.
Can anybody expain this.
class Test
{
Test(int i)
{
System.out.println("Test(" +i +")");
}
}
public class javaprog
{
static Test t1 = new Test(1);
Test t2 = new Test(2);
static Test t3 = new Test(3);

public static void main(String[] args)
{
javaprog Q = new javaprog();
}
}
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shikhar:
Hi,
I am confused wih the output of this code which is :
Test(1)
Test(3)
Test(2)
I think the code should give compiler error, because there is no zero argument constructor in the Test class.
Can anybody expain this.


The output is correct ! There's no question of having a zero argument constructor in the test class.
Test(1) and Test(3) is the result of the class initialization process.
Test(2) is displayed because of class creation process.
The compiler should NOT give an error !
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shikhar,
Let me try to help u out.


Hi,
I am confused wih the output of this code which is :
Test(1)
Test(3)
Test(2)
I think the code should give compiler error, because there is no zero argument constructor in the Test class.
Can anybody expain this.
class Test
{
Test(int i)
{
System.out.println("Test(" +i +")");
}
}
public class javaprog
{
static Test t1 = new Test(1);
Test t2 = new Test(2);
static Test t3 = new Test(3);

public static void main(String[] args)
{
javaprog Q = new javaprog();
}
}



As soon JVM load the class first time it it load and innitialze the static variables of the class in there order of appearence in the class.
In our case as soon as JVM load class in to memory it it alse initialze the static variables.
First it initialize t1 variable
static Test t1 = new Test(1); //prints Test(1)
Then t3
static Test t3 = new Test(3); //prints Test(3)

after that when we create the instance of class the instance variable of the class get initalized.
static Test t2 = new Test(2); //prints Test(3)
Hope i could help u. i fnot let me know i can try again.
vivek


[This message has been edited by Vivek Shrivastava (edited July 15, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vivek,
Actually, by mistake, I took "test" class as the superclass of "javaprog" class.
Now this is clear.
thanks again
shikhar
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U better to go through some basic java books.
then learn what is object & class?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic