1

I am not very familiar with java. I created a jersey web server. There is different functions such as startRodio(), stopRadio(), setRadioIp()... I created one RequestHandler class to handle the http requests and one other Radio class that implement them. All the properties and methods of the Radio class are static. it looks like

Radio

class Radio{

public static boolean radionOn;
public static String radioIpadress;


public static boolean startRadio(){
radioOn = true;
// some other operation
}
...

RequestHandler

classe RequestHandler {

@path(/startRodio)
.....
if (!Rodio.radioOn)
Radio.startRadio();

Is it a good architecture for my programm? is it a good practice to make all the properties and method static in this way?

1
  • What you call "static classes" is unrelated to static class (nested class) as you're meaning static class fields rather than classes. Avoid static fields as much as you can (apart from constants, they cause needless troubles). Commented Sep 27, 2016 at 13:53

5 Answers 5

1

I would say, that making properties static in default as you have made above is not good practice at all.

If you have only one instance of such object as Radio is, then use singleton pattern and private properties with proper getters and setters. This is generally best approach, because you separate public interface from private implementation and change in the implementation (e.g. renaming variable) would cause problems in other parts of application and need of refactoring.

Static variables should serve just for some common properties for defined type/class. You can for example count existing instances of class in static variable.

Sign up to request clarification or add additional context in comments.

Comments

1

Better avoid using static variables. This is not a good practice. Static variables have global scopes which leaves you testing so hard. Also anything can be able to modify the static variables. more over, using static is not thread safety. Also you don't have control over the static variable i terms of their creation and destruction. SO its not advisable to use statics.

Comments

1
  1. Just don't use static variables. It directly couples several of your classes.
  2. You can use singletons in place of static if you're sure that you need only one object.

Comments

0

Simply spoken: don't use static.

static is an abnormality in good OO design. It leads to direct coupling between your classes. It makes it hard to later replace "implementation"; and it makes it hard to write reasonable unit tests.

Meaning: by default, you do not use static. There might be situations when it is fine to use; but the example code you are showing does not at all look like you should be using static.

Instead, you should be defining an interface that denotes the functionality of your Radio; allowing for different implementations behind that interfaces.

Comments

0

It depends on what are you looking for.

Lets say you are creating 4 objects of Radio. radioOne....,radioFour...

Now if you want all Radios to start at same time, you should go for static variable because static properties are characteristics of all objects of a class. They are not exclusive to any particular object and in practice they should be assessed using class like :

Radio.radionOn=true;

and not radioOne.radioOn=true;

So, I would suggest you to make only those properties static which will be common to all objects. If all the properties will fall under that ambit, then it would mean you want only one object for the class because all your objects would behave the same .So better to have one object . In that case go for singleton pattern for object creation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.