• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static and non-static relationship question

 
Shay Gaghe
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Coders J
My program consist of several classes. I find it more readable,logic and useful to declare all the methods in all the classes �static� and get access by Class.member().
The problem showed up when I must use a java biuld-in method( as Component.remove ) within my static method , then I receive compile error.
The first alternative is to create the class instance inside the static my static method , the second is to use interface which declare its member as static final. The two ways doesn�t fit to my needs , do anyone know another way?
Thanks in advance
Shay
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is really hard to say without seeing any code, but if you pass a reference to the instance of the Component as a parameter to the static method and use it as a local variable you can act on it.
Many of the methods in the API are static and take in objects.
 
Shay Gaghe
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i feel like an OOP's traitor I�ve been told that I violate the OOP design�s rules in Java�s OOP design whenever I use only static members instead of creating class instances. Can anyone tell about the deficiencies and advantages of using the both form static and OOP?
thanks
Shay Gage
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I don't understand why you want to declare everything static. You say it is more "readable, logic and useful", but I don't know what you mean.
You need to be careful if you are declaring everything static. static means you only have one copy and if you have different code all calling the same static method you can get confusing results.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using all static code, it seems that you are only doing procedural programming and using classes as functions rather than using abstraction, polymorphism, inheritance, and encapsulation which are the cornerstones of OOP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic