• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

whizlab question

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output :00
05
please explain the output at line1,2,3?
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find the order of initialization in some good Java Books (like K&B SCJP6).
But for the purpose of your question, just know that before your assignment of instance variables, they are set to default values (i.e. int = 0, Object = null, etc.).

The instance variables assignment itself happens in up-to-down manner, so when you start initializing x variable, the y variable is set to default value (which is 0). When the assignment of x variable is finished, the y variable will be assigned the given value.

Hope that following this you can understand the rest of the code.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pedro Kowalski wrote:You can find the order of initialization in some good Java Books (like K&B SCJP6).
Hope that following this you can understand the rest of the code.


got your concept.
yes i am referring the same book.
this question was little tricky or my basics are still not cleared.
 
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, have you read all about instance initializers, I think a little hint you will need to solve it yourself.
First, the main() method is called, in that the constructor of the Main class which you wrote and all superclasse's contructors are invoked within this after that instance initializers and so on...........
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arjun Srivastava wrote:

Pedro Kowalski wrote:You can find the order of initialization in some good Java Books (like K&B SCJP6).
Hope that following this you can understand the rest of the code.


got your concept.
yes i am referring the same book.
this question was little tricky or my basics are still not cleared.



These questions are tricky, even if you read about initialization process but you see this type of question for the first time.

Next question basing on default instance variable values shouldn't be a problem to you :-)
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next time you will see such a question, you will be able to solve it. This is why we practice...and this is about learning the tricks which makes even the trickiest of the questions easy.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah ,i hope so.
 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to get 05 05 in the output ?


hi,Arjun Srivastava can you refer me the link to mock question ?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:how to get 05 05 in the output ?


hi mohitkumar gupta,why don't you try out,just twist the code .

BTW,this prints 05 05
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i still didn't get the answer.How is the program working.

x would be initialized before go is called i.e null
the y would be initialized to 5.
then what's the use of go method.

I have read about the order of initialization Kathy Sierra book but still can't get the code
please tell me how is it working ???
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:i still didn't get the answer.How is the program working.

x would be initialized before go is called i.e null
the y would be initialized to 5.
then what's the use of go method.

I have read about the order of initialization Kathy Sierra book but still can't get the code
please tell me how is it working ???



1st print statemnt in main:creates new object, x is initialized by calling go() which returns 0: output is 00

last print statement creates another object,so again x is initialized by calling go() which returns 0, and y is initialized to 5. output is 05
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic