java: non-static variable this cannot be referenced from a static context

genek

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;
    }
}
}

}

Nir Alfasi

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;
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

non-static variable cannot be referenced from a static context java

From Dev

non-static variable s cannot be referenced from a static context

From Dev

non static variable this cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Non-static variable cannot be referenced from a static context?

From Java

Java Generics: non-static type variable T cannot be referenced from a static context

From Dev

Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

From Java

java : non-static variable cannot be referenced from a static context Error

From Dev

error: non-static variable scan cannot be referenced from a static context in Java

From Dev

Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

From Dev

Java JTextField and non-static variable cannot be referenced from a static context

From Dev

Java Language | Error :- non-static variable scan cannot be referenced from a static context

From Dev

non-static variable this cannot be referenced from a static content Java

From Dev

Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

From Java

non static method cannot be referenced from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Java

non-static class cannot be referenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

non static setGravity cannot be referenced from static context

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Java

Java - cannot be referenced from a static context

From Java

Why do I get "non-static variable this cannot be referenced from a static context"?

From Java

"Non-static variable this cannot be referenced from a static context" when creating an object

From Dev

Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

From Dev

non-static variable this cannot be referenced from a static context issue when instantiating a new object

Related Related

  1. 1

    Non static variable cannot be referenced from static context java

  2. 2

    non-static variable cannot be referenced from static context [JAVA]

  3. 3

    non-static variable cannot be referenced from a static context java

  4. 4

    non-static variable s cannot be referenced from a static context

  5. 5

    non static variable this cannot be referenced from a static context

  6. 6

    "non-static variable this cannot be referenced from a static context"?

  7. 7

    Non-static variable filepath cannot be referenced from a static context

  8. 8

    Non-static variable cannot be referenced from a static context?

  9. 9

    Java Generics: non-static type variable T cannot be referenced from a static context

  10. 10

    Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

  11. 11

    java : non-static variable cannot be referenced from a static context Error

  12. 12

    error: non-static variable scan cannot be referenced from a static context in Java

  13. 13

    Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

  14. 14

    Java JTextField and non-static variable cannot be referenced from a static context

  15. 15

    Java Language | Error :- non-static variable scan cannot be referenced from a static context

  16. 16

    non-static variable this cannot be referenced from a static content Java

  17. 17

    Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

  18. 18

    non static method cannot be referenced from a static context

  19. 19

    non-static method getIntent() cannot be referenced from a static context

  20. 20

    non-static class cannot be referenced from a static context

  21. 21

    "Non-static method cannot be referenced from static context" error

  22. 22

    Non-static edit() cannot be referenced from a static context

  23. 23

    non static setGravity cannot be referenced from static context

  24. 24

    Non-static method getSocketFactory cannot be referenced from a static context

  25. 25

    Java - cannot be referenced from a static context

  26. 26

    Why do I get "non-static variable this cannot be referenced from a static context"?

  27. 27

    "Non-static variable this cannot be referenced from a static context" when creating an object

  28. 28

    Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

  29. 29

    non-static variable this cannot be referenced from a static context issue when instantiating a new object

HotTag

Archive