• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static and non-static relationship question

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic