What’s the deal with void and other Java basics?

0
5
Asked By CuriousCoder42 On

I have some basic knowledge of Java and a few complex concepts, but I'm still trying to piece it all together. I have a few questions that I hope someone can help clarify:

1. What exactly does 'void' mean in Java?

2. I understand that 'main(String[] args)' is for user input, but I'm not completely clear on how that works.

3. What's the difference between 'public static' and 'private static' methods since static is already associated with a class?

4 Answers

Answered By TechWhiz88 On

1. 'Void' indicates there is no return value from the function. Basically, it performs its task without sending anything back.
2. The 'args' is an array designed to hold command-line parameters that you've put in when you run the program. So when you type something like 'java MyProgram A B C', 'args' will contain those values 'A', 'B', and 'C'. This is how your program can use those inputs.
3. The difference between public and private static methodsing is mainly about visibility. A public static method can be called from other classes, while a private static method can only be accessed within the class it’s defined in.

Answered By CodeGuru99 On

Hey! So here's the breakdown:
1. A method declared as 'void' doesn’t return a value, which is pretty straightforward.
2. The 'args' parameter in 'main(String[] args)' is populated by the command line. Essentially, it lets you give your program inputs when you run it.
3. When you mark a method as 'public static', it can be accessed from other classes. On the other hand, 'private static' restricts access to the method within its own class only. Remember, it's all about who can call the method!

Answered By BeginnerBuddy On

I get where you're coming from, it can be confusing!
1. Just to clarify, 'void' means that method doesn't return any data when it's done running.
2. Regarding 'main(String[] args)', this is the entry point of your program where user inputs can be fed into it. Command-line parameters go into 'args' and are processed from there.
3. For the static method, public means it's accessible to everyone else in your code, while private means it can only be used within its own class. You're correct, static ties methods to the class itself.

Answered By JavaBuff123 On

1. In Java, 'void' signifies that a method doesn't return a value after it executes. It's essentially saying, "This method does something but won’t give you anything back."
2. The 'main(String[] args)' part means that your program can receive command-line arguments as input. When you run the program, whatever you input right after the program name gets passed into this 'args' array as strings.
3. The keyword 'static' means a method belongs to the class, not to individual objects. The 'public' part means other classes can access it, while 'private' means only the class itself can use it. So, it's about accessibility; public is for everyone, private is just for you! Moreover, private static methods can help organize your code without exposing everything outside.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.