0

I'm new to java and I have been exploring the different variable types. Right now I am trying to determine is printed when I add 1 to a byte variable of value 127 (the maximum value for a byte var). This is what my code looks like:

public class vars {

    byte b = 127;
    byte c = 1;

    public static void main(String[] args) {

    System.out.println(b + c);


    }
}

and on my System.out.println line I get the error message that my non-static variables, b and c, cannot be referenced from a static context. How should I fix this? Is there a better way to do this project in general?

Thanks

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Charlie Tidmarsh
  • 37
  • 1
  • 1
  • 2

1 Answers1

0

try this is code to use the member variable inside the main function you need the class object
vars v= new vars();

  public class vars {

            byte b = 127;
            byte c = 1;

            public static void main(String[] args) {
        vars v=new vars();
            System.out.println((v.b + v.c));


            }
        }
Nambi
  • 11,944
  • 3
  • 37
  • 49