-3

I want to know the meaning of static in my file. Everytime, I come across an error and then find out that the word static is missed. Can you please explain when and where should the word static be used.

Here, for example in the code, even if I remove static from the variable, it shows error.

Is, it static class, so it has to be static variable ??

public class TestJavaServer {

    static String xCordinate;

    public static void main(String[] args)
    {
                readFromFile();
        }
    public static void readFromFile() throws IOException
    {
                       xCordinate ="something";
       }
user3345483
  • 19
  • 2
  • 6
  • 2
    `static` means bound to class, not object (in context of Java) – mangusta Feb 28 '14 at 02:50
  • Static methods can only access static fields. – Mike Lowery Feb 28 '14 at 02:57
  • Don't downvote, the dude's learning Java from scratch – Ove Sundberg Feb 28 '14 at 03:18
  • @OverSundberg I generally do not downvote for lack of knowledge. I do downvote questions like "What does the static keyword mean?" because that horse has been beaten to death and nothing that StackOverflow answers will likely be better than what is currently out the on the internet, or even already on Stack itself http://stackoverflow.com/a/413904/3224483 – Rainbolt Feb 28 '14 at 03:22
  • @OveSundberg Thanks dude. But now, I cant ask question here at stackoverflow. Need to make other account now..thanks for the help though guys – user3345483 Feb 28 '14 at 03:34

1 Answers1

0

A static field is the same in all instances of the class.

Do

TestJavaServer.xCordinate ="something";

and Bob's your uncle.

However, rewrite your code like this instead:

public class TestJavaServer 
{
    String xCordinate;

    public static void main(String[] args)
    {
        TestJavaServer testJavaServer = new TestJavaServer();
        testJavaServer.readFromFile();
    }

    public void readFromFile()
    {
        xCordinate ="something";
    }   
}
Ove Sundberg
  • 323
  • 2
  • 10