In Java, can commands exist without methods? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In Java, can commands exist without methods?

Sounds foolish though but I need to ask, to be rest-assured. Would appreciate your response.

17th Dec 2018, 3:33 PM
Sol
Sol - avatar
8 Answers
+ 9
You are using methods when you use System.out.print( ); & System.out.println( ); There are two basic types of methods:  • Build-in methods are part of the compiler package, such as System.out.println( ); & System.exit(0);  • User-defined methods are created by you, the programmer. These methods take-on names that you assign to them and perform tasks that you create.
17th Dec 2018, 3:53 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
Raj Chhatrala 👍 Probably, I think so.
17th Dec 2018, 4:02 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 3
I don't actually understand your question. But what I know is that, in most of the programming languages every statement must be wrapped inside a method or function or maybe a class. You can not execute anything without using method. Because most of the programming languages starts with main method.
17th Dec 2018, 3:43 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
Danijel Ivanović maybe he means functions(block of code).
17th Dec 2018, 3:49 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
I dont really understand the question, but Maybe you can, check this out. public class Program { static{ System.out.println("surprise ! :D"); } public static void main(String[] args) { } }
17th Dec 2018, 4:34 PM
Taste
Taste - avatar
+ 2
Thats a static block You were asking about command without method right ? My example show that you can execute a block of code with out any method. A static block will run when the class is loaded. Sorry for not give much explaination :D
17th Dec 2018, 4:46 PM
Taste
Taste - avatar
+ 1
Thanks Raj, now I understand that a minimal program must contain at least one class and a function/method.
17th Dec 2018, 4:22 PM
Sol
Sol - avatar
0
Taste, you are trying to explain a problem with another problem. Thanks though
17th Dec 2018, 4:42 PM
Sol
Sol - avatar