• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Objects  RSS feed

 
rahul sengupta
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I would like to know about static obects in Java.
Since objects are created by instantiating classes. How can we have an static object in a class??

Thanks All of you In Advance for your help.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a "static object".

You can certainly declare a static variable:



And you can certainly assign an object to that variable:


However that doesn't make that File object a "static object". It just makes it an object which happens to be referred to by a static variable in some class.

As for the rest of your question, I don't understand it. Sure, creating that object would require instantiating the File class before the File object could be created. But I don't see why you think that might be a problem. Could you explain?
 
rahul sengupta
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second part was not my question.
Sorry for not being very clear about that. I was just trying to make it clear what I know.

Thank You for your help again. I understood a bit what you said.

But I have read in a book that an object can be static and an example was given that in case of
System.out.println()

println() is a method of object out which belongs to the class System.
And we can use the method println() without instantiating any object in System because the object out is Static.

I have read it in Ivor Horton's book.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rahul sengupta wrote:
But I have read in a book that an object can be static


That book was wrong. "Static" applies to members--variables, methods, nested classes--and to initializer blocks. It does not apply to objects. No object is ever static or non-static, just like it's not red or blue or tall or short.


and an example was given that in case of
System.out.println()

println() is a method of object out which belongs to the class System.
And we can use the method println() without instantiating any object in System because the object out is Static.


No. "out" is not an object. It is a variable. That variable has a value, which is a reference, which points to an object. The "out" variable is static. The object it points to is not. Objects do not have the property of being static or non-static. If you disagree, please provide a link to the part of the JLS that says otherwise.

I have read it in Ivor Hutons book.


Then either he was wrong, or you misunderstood what he wrote.
 
rahul sengupta
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO I do not disagree at all
I was confused about how can objects be static.
Thank you for your help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!