-1

I'm new to programming in Java (even though I'm used to code in C, html and some other languages (vaguely)).

I'm trying to create a simple Hello World program, and I'm unsure of why, and what the "static" tag does. I tried to do some research, and I came up with that the static tag makes the method work as an instance in a moment, instead of having to "launch" it (didn't quite understand it). I'm wondering where, and why to use it.

On the other hand, while I'm compiling, I found out how to make it execute, but some friends of mine have told me that I need to include a manifest, and run it as a .jar, (I'm using:

$ javac Potato.java
$ java Potato

)

As a note, I would like to tell you that I'm trying to avoid using 3rd-party softward to learn programming (using the standard Notepad++ and bash).

Thank you ;)

Edit: Sorry, this was my first question.

class Potato {
static String textA = "Hello There";
public static void main(String[] args){
System.out.println(textA);
}   
}

I got this code, and I did some mixing by creating the variable textA inside and outside of the main method.

mazunki
  • 682
  • 5
  • 17
  • 1
    How about sharing some code? – Mureinik Jan 10 '15 at 14:18
  • `static` means whatever is tagged with `static` is related to the class itself, not an instance of the class. That's it in a nutshell. – Dave Newton Jan 10 '15 at 14:21
  • @DaveNewton Isn't that the same as private/public tag? – mazunki Jan 10 '15 at 14:23
  • `static` is not a `tag`, neither is `public` or `private` they are `keywords` there is nothing in idiomatic Java terminology called `tag`. Making up your own terminology is not a good idea, only you will know what you are referring to. –  Jan 10 '15 at 14:29
  • 1
    You probably need to read some book about Java. IMHO Java is not such a thing that you can learn just trying to do something and maybe reading couple small tutorials. Here is too much important concepts and other things. And even if you get to write some working code on Java, it not likely to be a good code. – praetorian droid Jan 10 '15 at 14:30
  • 1
    **there is no valor in using notepad++ and the command line!** No professional or craftsman cripples themselves intentionally like that, and if you are having problems that could not be problems if you used the proper tools you are going to find very little help here. –  Jan 10 '15 at 14:31
  • Sorry, the word was keyword. I started reading the tutorial from Oracle, but old habits stay. The reason I use notepad and command line is just so I force myself to understand the why and how, not because I want to create anything fancy ;) – mazunki Jan 10 '15 at 14:36
  • 1
    @mazunki You will understand it more if you learn how to debug a program. – Tom Jan 10 '15 at 14:41
  • @mazunki No, that determines if the "thing being modified" has private or public access. – Dave Newton Jan 10 '15 at 14:47
  • 1
    @JarrodRoberson I strongly disagree. IMO it's important, at least for a short amount of time, to understand what the IDE is providing for you. Without any understanding of the classpath, how it interacts with your environment, etc. learning Java is harder. For toy, example programs without significant library use, a text editor and the command line is a great place to start. – Dave Newton Jan 10 '15 at 14:48

1 Answers1

1

The static modifier used on a method allows you to call such method on its class instead of a specific instance.

If you want to know why main is always static, have a look here: Why is the Java main method static?

Normal method

Definition:

public Class ExampleClass{
  public void exampleMethod(){
    System.out.println("exampleMethod()");
  }
}

Use:

ExampleClass exampleClassInstance = new ExampleClass();
exampleClassInstance.exampleMethod();

Static method

Definition:

public Class ExampleClass{
  public static void exampleMethod(){
    System.out.println("exampleMethod()");
  }
}

Use:

ExampleClass.exampleMethod();

The same applies to fields. If you have a static field, it will belong to the class and not to single instances.

Example:

public Class ExampleClass{
  public static int a = 1;
}

...

System.out.println(ExampleClass.a);
Community
  • 1
  • 1
0x5C91
  • 3,360
  • 3
  • 31
  • 46
  • I see, so I can create more instances of one same element using the Normal method, and by using the static one, I just use "the standard old unique variable"? If I'm right, how would I make another instance? – mazunki Jan 10 '15 at 14:31
  • The answere helped a lot, thank you. (I don't get the downvote either) – mazunki Jan 10 '15 at 14:33
  • Please consider that the adjective "normal" on a method was used to mean "without modifiers". Your question suggests that you don't have a clear understanding of the concept of "instance of a class". I can't clarify that here, I suggest you read some basic Java manual for your doubts in that direction. – 0x5C91 Jan 10 '15 at 14:35
  • 1
    Yep, I understood. I will search on that, thank you n.n – mazunki Jan 10 '15 at 14:38
  • 1
    @Numbers That's actually incorrect; you *can* refer to static properties/methods from an instance, you just *shouldn't*, because it's confusing. You're correct in that you can't refer to instance properties from a static context, because it would be meaningless. – Dave Newton Jan 10 '15 at 14:51
  • Thanks @DaveNewton for pointing that out, I removed my misleading comment. – 0x5C91 Jan 10 '15 at 14:57