Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static object" Watch "Static object" New topic
Author

Static object

Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

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?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

There is no static object, and static is a modifier(more accurately non-access modifier). Please TellTheDetails!~


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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"?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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.



SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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..
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

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

Joined: Mar 17, 2010
Posts: 1051

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


Have you tried this,i think this is not possible
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

OH COME ON it is ! i want to know whyyyyyy !! whats the use of it ?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

it has no use why not you just check it out.It will not even work and gives you compiler error.
Mina patpati
Greenhorn

Joined: Oct 01, 2010
Posts: 1
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

Joined: Mar 07, 2010
Posts: 438

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
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59






This is kind of an example where they are using a static reference. WHY?
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

It compiles in class scope
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jun 15, 2010
Posts: 59

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

Joined: Nov 04, 2009
Posts: 2066

Aashima Arora wrote:hey abhimaran,

Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

am terribly sorry ..
Thanks abimaran :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static object
 
Similar Threads
static block
what is the process if i make a object as static?
What is "non-static method" vs "static context" ?
Single tone Vs Static
Static