0

I new in Java (after python). And have a question about using one class without creating new instances. This code works, but it's good or bad habit to do like this?

class TestStatic {
    private static String url;
    private static String user;

    TestStatic(String _url, String _user) {
        url = _url;
        user = _user;
    }

    static void print() {
        System.out.println(url);
        System.out.println(user);
    }
}

public class Main {
    public static void main(String[] args) {
        new TestStatic("HTTP", "USER");
        TestStatic.print();
//        TestStatic test = new TestStatic("HTTP", "USER");
//        test.print();
    }
}

Or I need to comment first two strings in the main method, then uncomment next two and use them in code like this?

Tutunak
  • 13
  • 2
  • 9
    Very bad, every time you create a new `TestStatic`, your static fields will be overwritten. – daniu Jun 28 '18 at 14:07
  • 6
    Setting `static` field via constructor is mostly discouraged – Lino Jun 28 '18 at 14:08
  • If you need this logic, you can look for Singleton pattern in Java. And you don't need to create new instance of a class, you can just assign values to url and user. – Pospolita Nikita Jun 28 '18 at 14:08
  • 1
    You don't instantiate static classes. – NiVeR Jun 28 '18 at 14:08
  • 3
    This looks like an XY problem, What are you trying to achieve ? – vincrichaud Jun 28 '18 at 14:10
  • @daniu I will use this only one time in code, I won't create new TestStatic after this. – Tutunak Jun 28 '18 at 14:11
  • 1
    There is no reason to do this. Just create it as regular non-static object and pass the reference around. This leads to a far more flexible structure. The pattern itself is called **Singleton** (it looks slightly different though). But as said, usually you don't and shouldn't need it. – Zabuzard Jun 28 '18 at 14:14
  • Create something static (methods or fields) means they are related to the class and not to the instances. If your fields are used to define an instance, don't make them static and initialize them in your constructor. If your method is to manipulate an instance don't make it static. – vincrichaud Jun 28 '18 at 14:15
  • 1
    There is not necessarily any reason to use singleton. Just because you only ever need to use one instance of an object doesn't necessarily mean you should also *enforce* that only one instance can ever be created. – Radiodef Jun 28 '18 at 14:18

3 Answers3

0
new TestStatic("HTTP", "USER");

What are you trying to do with this?

You have created a static method inside a static class which can be accessed without creating an instance of TestStatic.

TestStatic.print();

The above line is fine, but your variables need to be static and PLEASE DO NOT SET THEM INSIDE A CONSTRUCTOR. no need to create a new object of test static, because you are doing nothing with it and its probably going to the garbage collection soon after creation.

Most people use static classes for utility functions. You can have a read on when to use static classes and when not to use them.

After clarification of your question, on creating a single class for whole project to connect it to SoniqMQ, you are better of with Singleton pattern.

Singleton pattern forces you to only create a single object of the class, and you can use this inside your whole project.

public class MySingleTon {

    private static MySingleTon myObj;

    private MySingleTon(){
    //Empty private constructor    
    }

    public static MySingleTon getInstance(){
        if(myObj == null){
            myObj = new MySingleTon();
        }
        return myObj;
    }

    public void getSomeThing(){
        // do something here
        System.out.println("I am here....");
    }

    public static void main(String a[]){
        MySingleTon st = MySingleTon.getInstance();
        st.getSomeThing();
    }
}
Farhan Qasim
  • 990
  • 5
  • 18
  • "static class" -> I don't see any here... static class is only for inner class – vincrichaud Jun 28 '18 at 14:11
  • I want to create one class for connection to SonicMQ and use this globally for all in my code. This is just example for a concept. – Tutunak Jun 28 '18 at 14:13
  • 2
    @Tutunak you are better of with creating a singleton pattern for the class. Through the singleton pattern, only one object will be created and you can use the same object for all the project. – Farhan Qasim Jun 28 '18 at 14:14
0

This is definitely not the way you want to use classes and static variables. It completely depends on how you want to use the specific class. If you want to have the same variables in all instances or no instances at all, you should use it like this:

class TestStatic {
    private static String url;
    private static String user;

    static void print() {
        System.out.println(url);
        System.out.println(user);
    }

    public static setUrl(String url){
        TestStatic.url = url;
    }

    public static setUser(String user){
        TestStatic.user = user;
    }
}

public class Main {
    public static void main(String[] args) {
        TestStatic.setUrl("HTTP");
        TestStatic.setUser("USER");
        TestStatic.print();
    }
}

So you wouldn't have to create instances at all. Just keep in mind that static variables are changed for ALL instances and that there are no instances needed to access/change them.

If you want the variables to be different for each instance you should approach it like this:

class TestStatic {
    private String url;
    private String user;

    public TestStatic(String url, String user) {
        this.url = url;
        this.user = user;
    }

    public void print() {
        System.out.println(url);
        System.out.println(user);
    }
}

public class Main {
    public static void main(String[] args) {
        TestStatic test = new TestStatic("HTTP", "USER");
        test.print();
    }
}

Note that you are not using static variables for instance-specific values.

Take a look at this for more information about static variables and methods.

As I read in your comment to Farhan Qasim's answer, your use case would (as he mentioned as well) need a singleton pattern. Just google what it is and what it does or just click this.

Naeramarth
  • 112
  • 13
0

As said in every answer and like all the comments. Don't mix static and instance initialization. if you still want to assign url and user you could create a static method for that:

final class TestStatic {
    private static String url;
    private static String user;

    private TestStatic(){}

    static void initialize(String _url, String _user) {
        url = _url;
        user = _user;
    }

    static void print() {
        System.out.println(url);
        System.out.println(user);
    }
}

public class Main {
    public static void main(String[] args) {
        TestStatic.initialize("HTTP", "USER");
        TestStatic.print();
    }
}

I also made the class final and its constructor private. Which prevent any instantiation of class TestStatic. Because it simply is not used and only holds static values.

Lino
  • 19,604
  • 6
  • 47
  • 65