This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static object

 
Aashima Arora
Ranch Hand
Posts: 59
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no static object, and static is a modifier(more accurately non-access modifier). Please TellTheDetails!~
 
Paul Clapham
Sheriff
Pie
Posts: 20199
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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"?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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..
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mina patpati
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles in class scope
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aashima Arora wrote:hey abhimaran,

 
Aashima Arora
Ranch Hand
Posts: 59
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am terribly sorry ..
Thanks abimaran :-)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic