• 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

Confusion with this Output

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When I run this code the output is coming as 1,2 which is okay but my question is in this code the d Dimension object is created only in the initialize method and also it is not static whereas in display method how it gets the reference and it prints the values of x and y. It must give NullPointerException right??? As mentioned in Kathy Sierra when we create a new object with a new keyword inside a method then that reference wont be available in other methods.

Does Dimension class objects works differently here ???

Can anyone help me whats going on here?

Thanks
Vijay

 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S. Vijay wrote:
When I run this code the output is coming as 1,2 which is okay but my question is in this code the d Dimension object is created only in the initialize method and also it is not static whereas in display method how it gets the reference and it prints the values of x and y. It must give NullPointerException right??? As mentioned in Kathy Sierra when we create a new object with a new keyword inside a method then that reference wont be available in other methods.

Does Dimension class objects works differently here ???

Can anyone help me whats going on here?

Thanks
Vijay




hi vijay,
first understand the concepts clearly.........
i assume you have just started learning java... right?

Dimension-d is not a local variable....it is an member of the class and can be called in any instance method...
as you said Dimension object was created within initialize method only, but its reference is visible to all instance methods as it is a member of prog8 class...

 
S. Vijay
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the reply so if that is the case in the Kathy Sierra book in threads chapter



When we run this code we get NullPointException right even here Chicks c is a member of a class if that is the case even here it must give proper output right?? I think its not due to use of threads we get this output.

Correct me if i am wrong.

Thanks
Vijay
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

here you have to know,
prog8 p = new prog8();

When JVM encounters new keyword,
object p of type prog8
will be created with its own set of instance variables and access to instance methods.

When you see instance methods , instance is running .
whereas in static methods, instance is not there.


In your example,
1. instance variable - d
2.two instance methods - initialize() and display(), both will have access to d.

As mentioned in Kathy Sierra when we create a new object with a new keyword inside a method then that reference wont be available in other methods.

- its only for local variables:
eg: Dimension d = new Dimension(1,2);
In your case its instance variable.



 
S. Vijay
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks priya and karthick

I figured it out now.
reply
    Bookmark Topic Watch Topic
  • New Topic