Forums Register Login

Static object

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
There is no static object, and static is a modifier(more accurately non-access modifier). Please TellTheDetails!~
+Pie Number of slices to send: Send
 

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"?
+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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



Have you tried this,i think this is not possible
+Pie Number of slices to send: Send
OH COME ON it is ! i want to know whyyyyyy !! whats the use of it ?
+Pie Number of slices to send: Send
it has no use why not you just check it out.It will not even work and gives you compiler error.
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send





This is kind of an example where they are using a static reference. WHY?
+Pie Number of slices to send: Send
It compiles in class scope
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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 !
+Pie Number of slices to send: Send
 

Aashima Arora wrote:hey abhimaran,


+Pie Number of slices to send: Send
am terribly sorry ..
Thanks abimaran :-)
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1373 times.
Similar Threads
Single tone Vs Static
static block
what is the process if i make a object as static?
What is "non-static method" vs "static context" ?
Static
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:10:43.