0

Hey guys I am not seeing what I am doing wrong here. I am currently following this tutorial on how to implement the Singleton Pattern for a project and I set up some test files to see if it would work out well with what I'm doing, and as far as I can tell I am following the pattern perfectly and yet I can't seem to access ANY of the methods, this is the third time I've set this up in various ways, and as this is the simplest setup and it still doesn't work right, I am in the dark on whats going on here. I just need what I'm doing wrong here pointed out if at all possible. Here is the code:

DCTest.java (the Singleton Class)

package com.saphiric.simproject.datacontrols;

/**
 * Created by Saphiric on 12/30/14.
 */
public class DCTest {

    // Singleton Test
    private static DCTest dct = new DCTest();

    private DCTest(){
        // Prevents outside instantiation
    }

    public static DCTest getInstance(){
        return dct;
    }

    // test variables for making sure it can have dynamic fields
    private int INT;

    protected void setInt(int newInt){
        INT = newInt;
    }

    protected int getINT(){
        return INT;
    }
}

DataCore.java (The file I want to access the Singleton Class)

package com.saphiric.simproject.datacontrols;

/**
 * Created by Saphiric on 12/29/14.
 */
public class DataCore {

    // Singletons Tests
    DCTest test = DCTest.getInstance();
    test.setInt(0);

    public DataController data = new DataController();
    public DecisionLocks locks = new DecisionLocks();
}
2
  • What lines in which classes are giving you what errors? Commented Dec 31, 2014 at 4:55
  • The test.setInt(0) is throwing the error. No matter how many times I revise this it never recognizes the methods of the singleton class Commented Dec 31, 2014 at 4:57

2 Answers 2

1

Your issue is that method calls in Java have to be in a method. So the issue you're having actually has little to do with the Singleton pattern at all, it's that you're trying to make a call in the body of a class, rather than a method. If you tried to compile the following you'd have the same error:

public class HelloWorld{
    System.out.println("Hello, World!"); //Err
}

The solution to your problem depends on what you are trying to accomplish.

If you're trying to call setInt(0) at the class load time of the DataCore class (and test was supposed to be a static field), use a static initializer (just the word static instead of a method header) for that statement.

public class DataCore {

    // Singletons Tests - static
    static DCTest test;

    //Called when the DataCore class is loaded.
    static{
        test = DCTest.getInstance();
        test.setInt(0);
    }
}

Alternatively, if the field test is actually supposed to be non-static, simply put the setInt call in a constructor:

public class DataCore {

    // Singletons Tests - nonstatic
    DCTest test;

    public DataCore(){
        test = DCTest.getInstance();
        test.setInt(0);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

So would setting up an interface for using the Singleton methods make things easier? This data is going to modifed and accessed ALOT in this project but needs to be accessible anywhere and never be reinstantiated, thus me looking into Singletons.
0

Enclose your code of DataCore class into a method.

public class DataCore {

    // Singletons Tests
    public void work () {                        // added code in this method.
        DCTest test = DCTest.getInstance();
        test.setInt(0);

        public DataController data = new DataController();
        public DecisionLocks locks = new DecisionLocks();
    }
}

1 Comment

Ive tried switching it to public with the same result. It does not recognize the mthods

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.