• 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

Static object

 
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is a static object ?
Those having read kathy sierra would know how frequently it has been used ..i want to know why in certain questions a null pointer exception is thrown when the object is not declared static?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no static object, and static is a modifier(more accurately non-access modifier). Please TellTheDetails!~
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashima Arora wrote:what is a static object ?
Those having read kathy sierra would know how frequently it has been used



Do you have an actual quote from the book including the words "static object"?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null pointer exception is through when the object is not having any value assgning to it..

Objects are having null as their default value..if you try to access null then it will through null pointer exception.


 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is not any static object..
we can use final modifier while declaring a object..but that too doesnot mean that object is final it means refernce variable point to object is final..
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:null pointer exception is through when the object is not having any value assgning to it..

Objects are having null as their default value..if you try to access null then it will through null pointer exception.




this may lead to confusion among all the beginners as many of the people don't know what actually objects is
for example


in this case many beginners think that the obj is the object
but this is not the case

new Object() is the object and the obj is just a reference variable that refers to the newly created object

 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am extremely sorry to write something as static object

what i meant to ask is

static myobject obj = new myobject();
assume my object is a class

here obj is the static thing. what is its use.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashima Arora wrote:
static myobject obj = new myobject();



Have you tried this,i think this is not possible
 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OH COME ON it is ! i want to know whyyyyyy !! whats the use of it ?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it has no use why not you just check it out.It will not even work and gives you compiler error.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you try to compile
static myObject obj= new myObject();

you will get compile error :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well answer to why is just that
we can apply static modifier to variables and methods and init blocks
well if you still don't get the answer please write some code
and then specifically tell us what exactly do you want to know
have a nice time
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Aashima,

It's easy to get terms confused! Wh don't we start with another situation in Java where "static" is used... can you give a different example of when you might use "static"?

Bert
 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





This is kind of an example where they are using a static reference. WHY?
 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles in class scope
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aashima Arora, you are asking, why is the word static used for the variable a in the animal class on line 13? Correct?

Static variables are belonging to Class. Not to a object of that class. So, this depends on out requirements!
 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey abhimaran, i just wanted to know the use of static references? for me , here "animals a" is static because it has to be referred from main which is a static context and its for the class as you said, not for a particular object.

well, sorry for not being very clear about the question. i dont have the book right now with me . i will post in some days for what exactly i want to ask. Till then , Thanks so much and adios !
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashima Arora wrote:hey abhimaran,


 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am terribly sorry ..
Thanks abimaran :-)
reply
    Bookmark Topic Watch Topic
  • New Topic