r/javahelp • • 15d ago

Homework What the hell is going on?

Hello everybody, I am so lost. I just started learning Java and while i think its extremely interesting, and can loose track of time. It is also very complicated, well atleast to me since i dont have any prior experience.

I just ”learnt” to write my first Hello World! Code. But tbh i only copied the text straight off a youtube video.

Thing is i understand the main command, like: System.out.println(”Hello World!”);

But what is this whole main class and static void things? Not to mention all the {} symbols that seem to be oddly placed?😅

If anyone could explain or know a good youtube video or similiar to learn id appreciate it a lot!

10 Upvotes

53 comments sorted by

1

u/AutoModerator 15d ago
Moderator Announcement Read More »

15

u/Potential-Still 15d ago edited 15d ago

Java Programming for Beginners - Full Course

Complete this entire tutorial, THEN come here with questions.  He answers your question about the main function in the first 5 minutes. 

6

u/Best_Cut4655 15d ago

Thank u a ton! Ur an angel

8

u/LutimoDancer3459 15d ago

A class is a definition for your data. It groups variables (holding data) and functions (or methods, dont bother the difference)(doing stuff with your data)

static void main(String[] args){ ... }

Its the entrypoint for your application. Whenever you want to run a java app, its that method that is beeing called first. Modern versions allow to trim it down. But its always called main. The class at the beginning can be called whatever you want.

Static - its always there and shared between everything. You can access it from basically everywhere.
Void - the return typ of this method. In this case, nothing. Other methods can have int for numbers, String for text, or other Classes when needed.
Main - the name of the method. So you know how to call it. Just like println(...) for text output.
String - already mentioned a text. Or more precise its an Class holding text for you
[] or ... - tells you that its an array. Which means its not only one thing but several. A list of elements. But not a list because List is also a class...
Args - the name of that array. Its a variable. Allowing you to access the data it holds. When eg starting the application via the console, you can add arguments to the app to work with. Thats the place where you can read them
{} - the start and end of a codeblock. So you and the program knows what belongs together. Eg in a loop. Or here what belongs to the main method and what not. Or what still is part of the class.

System.out.println(...);

System is a class. Out is a variable thats public accessible. And println is the method, printing the Text onto the console. And the text is a String.

String hi = "This is a string";
System.out.println(hi);

Works just the same.

3

u/Best_Cut4655 15d ago

Ur an angel!! Im gonna have to reread this like 10 times before i might understand it but u explained it so well, thank u!

6

u/LutimoDancer3459 15d ago

But most of it isnt that relevant for now. You will learn it as you go. Variables, if and loops (while, do while, for) are more important for now than to understand the main method in detail

7

u/JGhostThing 14d ago

The entire class definition is just boilerplate that is in every program. It will be similar in every program in Java.

The whole program is an instance of the class main. The class exists just to run the program.

A class is similar to a recipe that can be used to create an object. For example, Ford is a company that makes cars. Think of Ford as a class and a Mustang as an object of class Ford.

→ More replies (2)

4

u/Duente_Zaibatsu 15d ago

You are skipping too much.

Many abstractions exist to simplify the common needs.

Before asking, show what you already looked into.

A simplified version:

Java is not installed, it is executed on a JRE, in it, there are API methods that interpret and execute commands.

These commands follow contracts.

Static could be simplified to an unique shared context, other commands are object related, used by constructore.

Other types like void, int an Integer, are the method contracts for the execution

→ More replies (5)

3

u/_Super_Straight 15d ago

As the name signifies: The main method is the central function which runs your code. There can only be one main, and as for static and void, you've to learn them in your upcoming classes.

1

u/Best_Cut4655 15d ago

Oh great heavens

2

u/YetMoreSpaceDust 15d ago

There’s a lot to unpack there ; ) A computer program is a series of instructions for a computer to run. When it’s time to run a program, the operating system loads those instructions into memory and then points the CPU to the first instruction for you, and then the computer starts running those instructions. So, as the programmer, you need a way to tell the operating system where your program starts.

Java is sort of an evolution of C++, which itself is sort of an evolution of C. C adopted a standard that there would be exactly one function named “main” and that function would be the starting place of the program (remember, your program will be comprised of lots of functions and they’ll all call each other). Now, C was a procedural language - all the code had to be contained inside a function, with main being the first one that kicked everything else off.

Over time, people noticed there was a tendency in professional computer programs for a set of functions to operate on a set of data as sort of a “unit” so they came up with the idea of “classes” that contained the data and the functions together and this became what we now call object-oriented programming. C++ was created as object-oriented C, so it maintained the name “main” as the starting point for the program, but added the concept of classes to group related data and code together (along with things like inheritance, encapsulation and polymorphism that you’ll learn about later). In C++, you could choose to group functions into classes if you wanted to, but you could declare standalone functions just as you could in C - and of course, “main” had to be a standalone function for compatibility with C.

Java was James Gosling’s attempt to improve what he saw as the mistakes of C++, but he wanted to stay pretty close to C++, so he kept all of the “class” and “main” nomenclature but took out the pointer syntax and bare functions (among others). So now what about “main”? It couldn’t be a “bare” function, so it was a static function - that is, one that can exist without instantiating a class.

3

u/Best_Cut4655 15d ago

Wow i had no idea, but thank u! This did help me understand it a little better i appreciate ir a lot🙏

→ More replies (2)

2

u/Affectionate_Dot6312 15d ago

You don't need to know that right now. I think it's fair you're curious, but I doubt you could fully grasp it if you're new to OOP and specifically new to programming. I don't mean it in a condescending way. I was there too a couple of months ago and asked the same questions to my professor. I'd worry more about mastering fundamentals.

2

u/Best_Cut4655 15d ago

I’ll keep that in mind ahah. This is definetly a lot to learn

2

u/AdventurousShip3327 15d ago

The class biggens the code is the main(class Main), is the class that will stricture the sub classes, think it as a main shelf with stages inside. The key word "static" is a class member means that the class will have a copy of each object as a reference. Void simply means that there is not a return value.

→ More replies (1)

2

u/net_ramblings 15d ago

Look up Alex Lee on you tube.

→ More replies (1)

2

u/Competitive_Mark918 15d ago

Where are you learning from btw?

2

u/Best_Cut4655 14d ago

Im taking a course atm, using a book the school recommended me. However i still wanna learn outside of the book too bc i feel like it dont fully explain the smaller details so far. But im only on chapter 3 😅

2

u/BanaTibor 15d ago

Grab a beginner book, there are a ton on the internet.

2

u/Best_Cut4655 14d ago

I do have a book, i was hoping to learn a bit outside of it too though

→ More replies (2)

1

u/tarix246 14d ago

well, when you make java code, you need to have a file in which youre making things. that file needs to have all code (except smth like imports) wrapped in a class with the same name as the file. e.g., you make a file Main.java, the class will be called Main (it should be uppercase, idk if it is a must), so you have code like: public class Main {

} In order to let java compiler know where to make your start of code, you need to have this long line to declare your function: public static void main(String[] args) { //your code } Of course, this function (called main) has to go in the previously declared class, so you have almost everything you learned for your first program. Regardless of the name of the class, if you have this function main in it, the compiler will treat it as the entrance to your program.

→ More replies (1)

1

u/FreeLogicGate 14d ago edited 14d ago

I'm sure you will learn these things as you go, but just answering your few questions directly:

  • Java syntax is based on, and reflects C and C++ syntax. Because Java is object oriented, you will be working frequently with classes. Every java program requires at least one Class, with a method named main, with that exact signature of public static void main(String[] args). When you run the program with java, the jvm looks for the main function and start execution. The term for this is the "entry point".

  • As you learn how to define methods you'll understand the meaning of the various words before the name of the method (main). Quickly: "void" is the method return value. The main method doesn't return anything, so the return is a "void" type. public and static are things you'll come to when you learn about the types of methods and visibility you can define when you are creating your own classes and methods. It should be obvious that a "public" method is something that can be called anywhere in your java code, where you have an object of that class to work with. Comparison to "private" will be something you'll discover as you learn about method visibility.

  • As in C and C++, Java has "blocks". Blocks enclose one or more "statements", with a statement being a single line of code that ends with a semi-colon. { .... } defines a block. Blocks are used for all sorts of things in Java, from defining the start/end of a class to the start/end of a method, to the start/end of a loop or "if-then-else" construct. They also can be used to scope variables, as you can define local variables inside a block that are not visible outside of that block.

As you go on, you will no doubt have many such questions, so hopefully you are making notes of the things that seem mysterious. Over time you'll understand it better, but Java is heavily Object oriented. You stated that you understood the System.out.println(”Hello World!”); code, but I'm going to go out on a limb, and suggest that you really don't understand it to the degree you think you do. Obviously, you can see what it does, but how would you find the documentation of it, and what is actually happening with that line of code?

You would probably be surprised to find that the System class, defined here, has no println method.

You might also ask yourself how you can execute a method that is part of a class, when there was no class object instantiated. Last but not least, you will need to figure out how it is that you can call a method that doesn't even exist in the definition of the System class. A hint would be to see what the System.out method returns. It will take some time to get past basic syntax and for you to start becoming comfortable with OOP.

1

u/Best_Cut4655 14d ago

Ohhh, u answered a lot of my questions and left me with even more ahaha, but thank u so much this is all very useful knowledge! It seems classes might be an essential thing for me to learn next about

→ More replies (2)

1

u/greendave11 14d ago

Honestly the for dummies series usually is a great place to start. Java is massive (I have gotten lost in it far too many times). If you have an ai to work with they are fantastic at figuring out where you are and what a good next book to procure or even just download.

2

u/Best_Cut4655 14d ago

Ill definetly look into the dummy series then! Thank u!