4

I have error java: non-static variable this cannot be referenced from a static context when compiling the code in line Man m1 = new Man("a1", "b1", 11); How to fix that?

public class Solution
{
public static void main(String[] args)
{
    //create two object of every class here
    Man m1 = new Man("a1", "b1", 11);
    Man m2 = new Man("a2", "b2", 12);
    Woman w1 = new Woman("a11", "b11", 13);
    Woman w2 = new Woman("a22", "b22", 14);

    //output them to screen here
    System.out.println(m1.name + " " + m1.age + " " + m1.address);
    System.out.println(m2.name + " " + m2.age + " " + m2.address);
    System.out.println(w1.name + " " + w1.age + " " + w1.address);
    System.out.println(w2.name + " " + w2.age + " " + w2.address);
}

//add your classes here
public class Man
{
    private String name;
    private String address;
    private int age;

    public Man(String name, String address, int age)
    {
        this.name = name;
        this.address = address;
        this.age = age;
    }
}
}

}

Carcigenicate
  • 39,675
  • 9
  • 62
  • 99
genek
  • 113
  • 1
  • 4
  • Do you want help to understand what the error means, or simply help to fix the problem? A static context means you are not in an "instance" of a class (and hence it does not have any member data/functions etc) - therefore when you access a non-static variable (which would only exist for an instance of the class) it says you can't, as you have no instance. – Chris Cousins Jul 28 '17 at 18:58
  • Your Man class is not static. This means it must have a reference to the outerclass.main is static and has no implicit outerclass. The simple solution is make your Man class static. – Mayur Patel Jul 28 '17 at 19:17

2 Answers2

4

One approach

Declare Man class as static and you'll be able to access it from within main() which is static as well (not tied to any instance of class Solution):

public static class Man

Another approach

We can also leave class Man non-static and create an instance-level factory-method which will create instances of Man:

public class Solution {

    public static void main(String[] args) {
        //create two object of every class here
        Solution solution = new Solution();
        Man m1 = solution.createMan("a1", "b1", 11);
        Man m2 = solution.createMan( "a2", "b2", 12);

        //output them to screen here
        System.out.println(m1.name + " " + m1.age + " " + m1.address);
        System.out.println(m2.name + " " + m2.age + " " + m2.address);    
    }

    Man createMan(String name, String address, int age) {
        return new Man(name, address, age);
    }

    //add your classes here
    public class Man {
        private String name;
        private String address;
        private int age;

        private Man(String name, String address, int age) {
            this.name = name;
            this.address = address;
            this.age = age;
        }
    }
}
Nir Alfasi
  • 51,812
  • 11
  • 81
  • 120
-1
public class Main {

    public static void main(String[] args) {
        // write your code here
        VerySimple alice = new VerySimple();
    }

    class VerySimple {
        private int age;
        private String name;
        //constructor

        VerySimple(int age, String name) {
            this.age = age;
            this.name = name;
        }

        //getter method
        int getAge() {
            return age;
        }

        //setter method
        public void setAge(int a) {
            age = a;
        }

        //getter
        String getName() {
            return name;
        }
    }
}
zmag
  • 7,281
  • 12
  • 30
  • 38
gukahhh
  • 1
  • 2
  • Don't waste your time answering old beginners questions that *should have been* closed as dups years ago. Also, code-only answer are not much help. You should start by explaining the point your code is designed to illustrate. The explanation should be the real answer; the code is just the illustration. – Stephen C Mar 03 '19 at 00:24