-3

I am creating a simple Jave console application. So I am using few classes in different files. I need to set some parameters for the class instance and retrieve it later. but it gives me following error. Please help me to solve it.

Main Class

public class Main {

    private Student student = new Student();   

    public static void main(String[] args) {

        student.setName("John");
            //Java: non-static variable student cannot be referenced from a static context

    }
}

Student Class

public class Student {

    private String name;

     public String getName() {
        return name;
    }

    public void setName(String name) {
        name = name;
    }
}

How can I avoid this issue ? Please help

Jens
  • 67,715
  • 15
  • 98
  • 113
D.Sachi
  • 51
  • 1
  • 1
  • 2
  • There's no *instance* of `Main` available in the function `main`. – Bathsheba Aug 22 '17 at 11:56
  • 1
    declare variable student as static – amicoderozer Aug 22 '17 at 11:56
  • This is a classic newbie error, a `static` method can be called directly from a class. Anything which is not static needs to be called from an instance Object of the class. Your Field `private Student student = new Student();` only exists for an instance object of Main. Like: public class Main { private Student student = new Student(); public static void main(String[] args) { Main main = new Main(); main.student.setName("John"); } } – MartinByers Aug 22 '17 at 12:06

1 Answers1

0

Like this:

public class Main {

    public static void main(String[] args) {
        Student student = new Student();   
        student.setName("John");
    }
}

You could have declared that private data member as static, but there's no reason for a class member at all. This class is nothing more than a driver. Better to instantiate all the objects it needs inside the main method.

Do yourself a favor and learn the Java coding standards. You are not following them in this code.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Why on earth is this downvoted? It's perfectly correct. – duffymo Aug 22 '17 at 11:58
  • I haven't voted, but these votes reflect usefulness, not necessarily correctness. – Tom Aug 22 '17 at 12:28
  • Usefulness? I think the OP should judge that. Just learning about the Java coding standards is worth the price of admission alone. Simple answers can still be useful to the person asking. – duffymo Aug 22 '17 at 12:34
  • You might haven't noticed yet, but you don't answer for OP alone and Stack Overflow isn't a help desk, thus helpfulness obivously also considers future readers. – Tom Aug 22 '17 at 12:58
  • I don't need instruction, especially not from you; I've been here long enough to know how it works. SO is a Q&A site for programmers. A user posted a question; I gave an answer. It's not the first or the last time that someone who's not familiar with Java will get confused about static and non-static. Do you make a contribution here besides trollish comments? Or do you answer questions, too? – duffymo Aug 22 '17 at 12:59
  • If a question is worth an answer (not a dupe, clear, useful and not answered yet), then I'll answer it. **"I've been here long enough to know how it works."** your third comment proved your wrong :/. But nevermind, no need to continue that discussion. – Tom Aug 22 '17 at 13:10
  • We'll all look forward to that. Not many questions appear to have met your high standards in the three years you've been here. (PS - I believe it's "proved", not "proofed". I know how it works.) – duffymo Aug 22 '17 at 13:11
  • Yes that's true and kind of sad that these rules are considered "high standard" and not "default" for a community who tries to create high quality content. – Tom Aug 22 '17 at 13:12
  • I think the community has done very well without you. Sad? Not at all. Stack Exchange is doing very well, indeed. – duffymo Aug 22 '17 at 13:22