• 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

Exercise for OCA exam inspired by myself about class members and initialization by default

 
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!. Just because many of you guys are helping me for preparing for my exam I want to "contribute" with all of them in my same situation. So this is one to practice:


I don't want to write the right answer so you can solve it and then compile and check your own one.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a question from a book, or did you write it yourself? Please always tell us where such questions come from.
What difference would it make if you wrote nro++; in line 4?
Is nro short for numero or similar? Is that Italian for, “number”?

I got the question right, but I think it best I move you to the exam forum.
 
Ranch Hand
Posts: 95
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pablo Napoli wrote:Hi everyone!. Just because many of you guys are helping me for preparing for my exam I want to "contribute" with all of them in my same situation. So this is one to practice:


I don't want to write the right answer so you can solve it and then compile and check your own one.

pablo what you are trying to explain in the code
What ever the program says that it's output is
1
1
Because you have created a static variable so whenever class is loaded static fields and static block are executed in the order they are specified in the class
Next object is created and make method is called twice so output is 1 for first method call and same for next call
It is good for practice purpose
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . Next object is created . . .

Please be specific. Which objects are created? How many? Is the make() method called once per object or twice?
 
Pablo Napoli
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. thanks for replying

Ritchie, about your questions, this is an exercise made by myself and what you say about if there is any deference, in this case, between ++nro and nro++, it's the same result. And yes, the name of the variable should have been num (I used the spanish acronym).
 
sohail hussain
Ranch Hand
Posts: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

sohail hussain wrote:. . . Next object is created . . .

Please be specific. Which objects are created? How many? Is the make() method called once per object or twice?

two objects are created, for every new test object make method executed
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That still isn't clear, but it does look correct to me

[edit]Two instances of the Test class. The make() method is called once on each instance (=2×). No instances of Test2; none is required because its one member is static.
reply
    Bookmark Topic Watch Topic
  • New Topic