Ria B
Ria B

Reputation: 19

two non-static variable this cannot be referenced from a static context error

I'm doing this code right now and it always returns the error "non-static variable this cannot be referenced from a static context" on the part "Teacher x1 = new Teacher(); and Student y1 = new Student()". I'm confused on what I should do to make the program work. Thanks for the help!

edit: added the other classes edit 2: The goal of this code is to have a class Person (with name, age, and gender), another class Student (with grade), and Teacher (with salary). The Student and Teacher classes must inheritance from Person class.

My code:

import java.util.*;
import java.util.Scanner;

public class Java2Technical {

public static void main(String[] args) {
System.out.println("DATABASE");
System.out.println(" ");
System.out.println("1. Teacher");
System.out.println("2. Student");
    
System.out.print("Choice: ");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();   
System.out.println(" ");
    
if(choice == 1) {
    Teacher x1 = new Teacher();
    System.out.print("Name: ");
    Scanner scan1 = new Scanner(System.in);
    x1.name = scan1.nextLine();
    System.out.println(" ");
    
    System.out.print("Age: ");
    Scanner scan2 = new Scanner(System.in);
    x1.age = scan2.nextInt();
    System.out.println(" ");
    
    System.out.print("Gender: ");
    Scanner scan3 = new Scanner(System.in);
    x1.gen = scan3.next().charAt(0);
    System.out.println(" ");
    
    System.out.print("Salary: " + x1.salary);
    System.out.println(" ");
    }
    
    else if(choice == 2){
    Student y1 = new Student();
    
    System.out.print("Name: ");
    Scanner scan1 = new Scanner(System.in);
    y1.name = scan1.nextLine();
    System.out.println(" ");
    
    System.out.print("Age: ");
    Scanner scan2 = new Scanner(System.in);
    y1.age = scan2.nextInt();
    System.out.println(" ");
    
    System.out.print("Gender: ");
    Scanner scan3 = new Scanner(System.in);
    y1.gen = scan3.next().charAt(0);
    System.out.println(" ");
    
    System.out.print("Grade: " + y1.grade);
    System.out.println(" ");
    }
    else{
        System.out.println("Invalid Input");
    }
}
     public class Person {

     String name;
     int age;
     char gen;
     double grade;
    double salary;
 }

public class Student extends Person {
    public void setName(String Brand) {
        this.name = name;
   }
public void setAge(int age) {
    this.age = age;
}
public void setGen(char gend) {
    this.gen = gen;
}
public void setGrade(double grade){
    this.grade = grade;
}
public String getName(String name) {
    return this.name;
}
public int getAge(int age) {
    return this.age;    
}
public char getGen(char gend) {
    return this.gen;
}
public double getGrade(double grade) {
    return this.grade;
    }   
}
public class Teacher extends Person {
public void setName(String Brand) {
    this.name = name;
}
public void setAge(int age) {
    this.age = age;
}
public void setGen(char gen) {
    this.gen = gen;
}
public void setSalary(double salary) {
    this.salary = salary;
}
public String getName(String name) {
    return this.name;
}
 public int getAge(int age) {
    return this.age;
}
  public char getGen(char gen) {
    return this.gen;
}
  public double getSalary(double salary) {
    return this.salary;
    }  
}
}

Upvotes: 0

Views: 145

Answers (1)

Polygnome
Polygnome

Reputation: 7795

Your main method is static, so you are in a static context where this does not exists.

A static member or static method is associated with the class, not an instance of the class. Static members exists once per class, non-static members are unique for each instance.

If you have an inner class like you do in your example, you can only create an instance of the inner class if you already have an instance of the outer class.

public class Outer {
    public class Inner {  }

    public static void main (String[] args) {
        var outer = new Outer();
        var inner = outer.new Inner(); // notice the use of the outer instance here
    }
}

If you do not wish to bind every instance of the inner class to a specific instance of the outer class, and want to be able to instantiate it independently, you need to make the inner class static:

public class Outer {
    public static class Inner {  }

    public static void main (String[] args) {
        var inner = new Inner(); // works now
    }
}

Note the static keyword on the inner class, it makes instantiating the outer class unecccesary.


As an aside, you do not need to explicitly use the outer class when you are already in an instance context:

public class Outer {
    public class Inner {  }

    Inner inner;

    Outer() {
        var inner = new Inner(); // `this` exists, we are in an instance of the outer class and can thus instantiate the inner class because we are in the proper object context
        this.inner = inner;
    }

    public static void main (String[] args) {
        var outer = new Outer();
        var inner = outer.inner; // access the outer classes field and get the instance of the inenr class that was created in the constructor of the outer class
    }
}

Please also note that proper indentation makes your code much easier to read.

Upvotes: 1

Related Questions