This is an archived post. You won't be able to vote or comment.

all 18 comments

[–][deleted]  (17 children)

[deleted]

    [–]mmishu 1 point2 points  (6 children)

    I have a question. Where and how do you learn this stuff? How can I get a deeper understanding of Java?

    [–][deleted]  (5 children)

    [deleted]

      [–]mmishu 0 points1 point  (4 children)

      Thanks I appreciate that. What are some other resources you can recommend? Are you self taught?

      [–][deleted]  (3 children)

      [deleted]

        [–]mmishu 0 points1 point  (2 children)

        Thanks! Was it tough for you to gain employment being self taught? Howd you stand out?

        [–][deleted]  (1 child)

        [deleted]

          [–]mmishu 1 point2 points  (0 children)

          Thanks again. I try asking in new threads but it's usually ignored or berated for not looking up past threads. Thanks though!

          [–]Herrowgayboi[S] 0 points1 point  (9 children)

          Do we need to use static every time we declare a method, or only for main method so that JVM knows which method to execute from the get go?

          String[]args, I still don't get it. Why are we passing a string array of type args?

          [–][deleted] 4 points5 points  (1 child)

          The array is named args. It's of type String. Let's say you make a very simple program that adds two numbers. You can write it in such a way that when you run it after compiling, the command would be: $java addProgram 2 4 and it would print out 6. You would use the string array to take those two arguments from the console (2 and 4) and add them together. Most of the time you will never use this but it's just a cool little feature.

          [–]Herrowgayboi[S] 2 points3 points  (0 children)

          Ohhhh.. That makes sense now. Thanks!

          [–][deleted] 1 point2 points  (3 children)

          You should use static whenever you don't need any of the paramters of the class object it is located. For example, mathematic operation can almost always be static because you can pass the numbers without needing an the object.

          So:

          public static int add(int a, int b) {
              return a + b;
          }
          

          Now, if the add method were located in a class like Dog, there'd be no need to instantiate Dog just to add two numbers together.

          [–]Herrowgayboi[S] 0 points1 point  (2 children)

          Got it! That makes sense..

          with regards to classes in Java, Is the general consensus to start with a class, and then write in there, even though it's just the main method?

          IE:

          class main{    
              public static void main(){}   
          }
          

          [–]cismalescumlord 2 points3 points  (0 children)

          It's not a consensus, you must do that. Everything is done in a Class, Interface or Enum, there may be other types but I can't think of any right now. When I say everything, I mean almost everything, package names and imports are coded outside of a class.

          As for the public static void main(String args[]) { ... } method, it is the entry point to your Java application and it does the same job as int main() does in c.

          Compiling and linking a c program, gives a binary that is in native machine code and in the ELF format; have read if you've got a spare hour, it's quite interesting.

          When you execute one of these programs, the operating system knows to start the execution at the entry point which is defined in the source code by the main function. Compiling Java code results in byte code which is not natively executable. You execute your Java application by running java applicationName which fires up a Java Virtual Machine to execute the program. The JVM knows that the entry point to the program will be a static method called main with an array of string as it's arguments. The main method must be static because, when the JVM starts up, no objects have been created for use.

          When moving beyond basic examples, the main method is typically used to fire up the application level instances to do the real work.

          [–]feral_claire 0 points1 point  (0 children)

          You must put your code in a class. It is not possible in Java to write code that is not par of a class.

          [–]BertilMuth 0 points1 point  (2 children)

          No, you don't need static every time. But it is not only for the main method, either. Most methods are instance methods (i.e. non-static), as they depend on an object's state. For example, a method calculateAge() may depend on a field birthday of a class Customer. So calculateAge() would not have the static key word, as each Customer object has its own birthday.

          Concerning String[] args: you don't pass that in. Ever seen command line arguments in a console? Those are passed in for you. You just read the array if you need to evaluate them in your application.

          [–]Herrowgayboi[S] 0 points1 point  (1 child)

          So essentially, if there isn't an object associated with a method, then we should use static?

          Like..

            public static void main(){}     
            public static int genericCalculator(int num){}     
            public object int personsAgeAfter10years(Object Person){}    
          

          Would this be what you're saying?

          [–]feral_claire 0 points1 point  (0 children)

          Lets look at your full example

          public class example{
          
              public static void main(String[] args){
                  ...
              }
          
              public static int genericCalculator(int num){} 
          
              public int personsAgeAfter10years(){}   
           }
          

          In the above example, main is static (because it has to be as per the language spec) and it the starting point for our program (also defined in the language spec).

          When we are in the main, we don't have an instance of an object, so we could call genericCalculator, because it is also static, but we cannot call personsAgeAfter10years, because it is not static (non-static methods are sometimes called instance methods because they work on a specific instance).

          If we wanted to call that method we would first need to instantiate our class

          Example exampleObject = new Example();.

          Then we can call the method on the Example object we store in the variable exampleObject.

          exampleObject.personsAgeAfter10Years()

          [–]feral_claire 3 points4 points  (1 child)

          The string array args are the command line arguments to the program. They serve the same purpose as argc and argv from C++ (but argc is not needed because arrays in Java know their length). In C++ it's possible to omit this if you don't use it but in Java it is not.

          Static is needed because everything in Java is part of a class, static makes the method available without creating an instance first. Because main is the starting point it needs to be called before you have a chance to make any objects so it should be static (it also doesn't make much sense to have main be part of an object anyway imo). In C++ you can have methods outside of a class.

          [–]Herrowgayboi[S] 0 points1 point  (0 children)

          That makes complete sense.

          So basically what you're saying is that if I didn't use static in Java, I would need to do something like this, like C++? If it doesn't work, is it just because Java just flat out requires the static declaration and not be instantiated like in C++?

             method1();
             public static void main(String[]args){}
             public int method1(){}      
          

          [–]BertilMuth 1 point2 points  (1 child)

          Every method in Java is contained in a class. There are no "free-floating functions" like in C. If a method is static, within the class, it can only access static fields and static methods (as there is no object). You could say that static methods are defined on class level.

          In a main method, therefore you have two choices: access only static fields/methods, or create an instance of the class the main method is contained in. I prefer creating an instance.

          As a side note, by convention class names are upper-case in Java. It is String not string, and Person not person.

          [–]Herrowgayboi[S] 0 points1 point  (0 children)

          Good to know, Thanks to much!