-2

i am trying to pass the value t from one class to another, but before i even run the program i get non static method cannot be referenced from static context from this line of code :

t = (PrinterSettings.getT() * 60);

i am trying to get the value t from this code :

public int t = 1; //defualt value for amount of mintues in the future the job should wait untill sent

public int getT() {
            return (t);
        }

 public void setT(int t) {
            this.t = t;
         } 

what have i done wrong ? and how can i get t

EDIT :

Whole of my code where i get t from

         public int t = 1; //defualt value for amount of seconds in the future the job should wait untill sent

    public int getT() {
        return (t);
    }

    public void setT(int t) {
        this.t = t;
    }

and this is the class that i am using that calls t from the above class to use:

public class DealyTillPrint {

    public int t;

    public String CompletefileName;
    private String printerindx;
    private static int s;
    private static int x;
    public static int SecondsTillRelase;

    public void countDown() {
        System.out.println("Countdown called");
        s = 1; // interval 
    t = ((new PrinterSettings().getT()) * 60); //(PrinterSettings.SecondsTillRelase); // number of seconds
        System.out.println("t is : " + t);
        while (t > 0) {
            System.out.println("Printing in : " + t);
            try {
                Thread.sleep(s * 1000);
            } catch (Exception e) {
            }
            t--;
        }

and here is where i set t using a spinner

<p:spinner min="1" max="1000" value="#{printerSettings.t}"  size ="1">
                    <p:ajax update="NewTime"/>
                </p:spinner>
user2061913
  • 910
  • 2
  • 14
  • 34

3 Answers3

2

You're using PrinterSettings.getT() but you can't do that because PrinterSettings is a class and the getT() method is for the object. You need to create an object of PrinterSettings first, then you can call getT().

PrinterSettings myObjectOfPrinterSettings = new PrinterSettings();
myObjectOfPrinterSettings.getT();  //this should work without the error
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
1

You can choose to do 1 of 2 things:

1) Make everything in your PrinterSettings file static (and make PrinterSettings static as well):

public static int t = 1; 

public static int getT() {
            return (t);
        }

 public static void setT(int t) {
            this.t = t;
         } 

2) Don't change PrinterSettings, and just do this for your code:

//Put this somewhere at the beginning of your code:
PrinterSettings printerSettings = new PrinterSettings();

//Now have some code, which will include setT() at some point

//Then do this:
t = (printerSettings.getT() * 60);

In my opinion the latter would be more preferable.

EDIT: The edit that I just made is because if you don't keep a hold on the PrinterSettings variable that you were using, new-ing one up will have t be 1 in that new PrinterSettings object. Instead, make sure that you're instantiating an object of PrinterSettings at the beginning of your program, and just use that one the whole way through.

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
0

Instead of:

public int getT() {
            return (t);
        }
Write:

public static int getT() {
            return (t);
        }

This will solve your problem. With this change you can access this method with its class name. As a class method.

abson
  • 9,148
  • 17
  • 50
  • 69