• Post Reply Bookmark Topic Watch Topic
  • New Topic

java STATIC fields.  RSS feed

 
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I dont know exactly what STATIC means.

here are a fow questions about static objects.

->doing all my application in a static context, that means all methods are static methods, is there anything bad on this?

->defining static fields: if I define the value for a static field on my class, next time i call this class will the fields keep that value? detail: all the static fields are declarated as

-> what does it means for an object to be static???
-ok, it means it will only be one instance of this object. but will this instance keep existing from different consecutive calls to the class that owns them?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucas, the static keyword means that the field or method is associated with a class, rather than an object. For example, in the following class, the 'shared' field is shared by all instances of the class:



Static fields don't necessarily stay the same between accesses, which is why they can be dangerous to use. If it really matters that they stay the same, use the final keyword, too.

For your last question, I think you are talking about Singletons, not static objects.

John.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might not have any instances if you are working only in a static context.

But doing everything in a static context is moving out of the realms of object-oriented programming altogether.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would severely limit the types of constructs that you can create. Lets take a classic example of a Person with a name ans age:



If you make all the fields and method in Person class static, there would only be a single value for Person.getName() and Person.getAge(). That's not very useful. You could do some things maybe with parallel arrays that would allow you to mimic a similar data structure, but that would just end up being a maintenance nightmare. Java is an object oriented language, go ahead and use stateful objects to model your domain.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, but my aplication has the objective to be executed by another, only executed, never instantiated.
a DATAFLEX program will make the java call for the jar, i have no intention that my appliation gets used as a API, it'll only be used for calling the main class, thats it.
I mean, there will be no class that instantiates my class, it's only a main class with 10 ~ 15 methods, and all methods are ment to be used inside the class, being, in this case, private methods.

in this case, using a static context (making all methods to be static), is a good call?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!