Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a non-static (dynamic instance) object as return for a static method in Java? [closed]

I have searched through stackoverflow as well as a few other sites and sadly have not found this question asked, let alone answered. Maybe my approach is best attempted another way? I am new to Java; this should be a really easy answer I would think.

The issue: I have a static method that I would like to return values from. For convenience and neatness I would like to use my own class instead of an ArrayList, String[], or similar. Problem is that I cannot instantiate my class within the static method (as I expected could be an issue). Funny thing though: using String[] or Object as the return does work (which is an instance of those classes)... so why can't I use my own class instance?

Example:

public static String[] decodeText(String codeString) {
    //Parse codestring and return values (not included in this example)
    String[] data = new String[3];
    data[0]="This";
    data[1]="does";
    data[2]="work";                
    return data;
}

The above works great but when I use my own class to return values the compiler gives me the "non-static variable this cannot be referenced from a static context" (NOTE: edited to show that these classes are nested within the JInputs class which apparently is necessary to reproduce the error):

public class JInputs extends JOptionPane {    
    //A lot of missing code here (which shouldn't be necessary to reproduce issue)

    public class UserData {
        public String userName;
        public String code;
        public long milliTime;

        UserData() {            
        }
        UserData(String userName, String code, long milliTime) {
            this.userName = userName;
            this.milliTime = milliTime;
            this.code = code;
        }
    }

    public static UserData decodeText(String codeString) {
        //Parse codestring and return values (not included in this example)
        UserData data = new UserData();
        data.milliTime = System.currentTimeMillis();
        data.code = "blah";
        data.userName = "Me";                
        return data;
    }
}

Obviously, I could make my UserData class a static class but then wouldn't subsequent calls to the method change the values of the original call? How do Java programmers return neat data from static methods? Why does it allow built-in classes to be instantiated but not user defined classes?

like image 283
DatuPuti Avatar asked Jun 02 '15 17:06

DatuPuti


People also ask

Can we use non static variable in static Block?

In the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.

Can a static method return a non static object?

The short answer to the topic question is no. You can't access non-static fields and methods from a static context in Java. You can do only oposite: reach statics from object instance.

Can you call a static method from a non static instance method?

A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class.

How can we use non static variable in static method in Java?

The only way to access a non-static variable from a static method is by creating an object of the class the variable belongs to. This confusion is the main reason why you see this question on core Java interview as well as on core Java certifications, e.g. OCAJP and OCPJP exam.


1 Answers

The only problem this code has is a misplaced curly bracket:

public class UserData {
    public String userName;
    public String code;
    public long milliTime;

    UserData() {            
    }
    UserData(String userName, String code, long milliTime) {
        this.userName = userName;
        this.milliTime = milliTime;
        this.code = code;
    }
} //end of class!

//this method is outside the class!
public static UserData decodeText(String codeString) {
    //Parse codestring and return values (not included in this example)
    UserData data = new UserData();
    data.milliTime = System.currentTimeMillis();
    data.code = "blah";
    data.userName = "Me";                
    return data;
}

I imagine what you want instead is this:

public class UserData {
    public String userName;
    public String code;
    public long milliTime;

    UserData() {            
    }
    UserData(String userName, String code, long milliTime) {
        this.userName = userName;
        this.milliTime = milliTime;
        this.code = code;
    }


    public static UserData decodeText(String codeString) {
        //Parse codestring and return values (not included in this example)
        UserData data = new UserData();
        data.milliTime = System.currentTimeMillis();
        data.code = "blah";
        data.userName = "Me";                
        return data;
    }
}

The above works great but when I use my own class to return values the compiler gives me the "non-static variable this cannot be referenced from a static context"

The code you posted does not result in that error. You either copied the code wrong, or you're looking at an old error.

Obviously, I could make my UserData class a static class but then wouldn't subsequent calls to the method change the values of the original call?

There really isn't a concept of "static class" the way you're describing. A static class is simply an inner class that can be accessed without an instance of the outer class. All of its members still act like the members of a normal class.

How do Java programmers return neat data from static methods? Why does it allow built-in classes to be instantiated but not user defined classes?

What you posted would work fine. Java does not make a distinction between "built-in" classes and "user-defined" classes.

like image 53
Kevin Workman Avatar answered Sep 17 '22 13:09

Kevin Workman