Do I need to create a Java class every time?

I'm starting to learn the Java programming language, and I'm a little confused.

I would like to create a simple program, for example, add two numbers or calculate the sum of n numbers. I can do this in C or Python easily, but in Java I have to create a class for my program and then create the main class and call my program from it.

I use both Netbeans and IntelliJ.

Can I just create a program directly, how to do it in other languages? I mean the concept of a class needed in Java?

+5
source share
5 answers

Can I just create a program directly, how to do it in other languages?

Not.

I mean the concept of a class needed in Java?

Yes. Each method, field, etc. It is always in a class (or interface). Yes, this is overhead for tiny programs, but for larger programs, the impact is pretty small.

As always, use the right tool for the job - if you want a script of several lines, use a scripting language. If you want more structure and organization, then you should expect a little โ€œceremonyโ€ to go with it.

+13
source

Java requires that every function / method be defined in a class. This includes the basic method.

The restriction is not imposed by all object-oriented languages. In some cases, the restriction is removed simply as a convenience (i.e. Python, Ruby). Some languages, such as JavaScript and Lua, provide OOP functions through a prototype-based mechanism. Java provides OOP with a class system, so you may hear that this is called a class.

+2
source

While you need to create a class, the question asks if you need to create a class for your program and the main class to call it. The answer is no.

You can create one class with the main method and have your own logic inside if you want a very minimal program. Something like that:

public class MyClass { public static void main(String[] args) { // Do Something here } } 
+2
source

Java is an object-oriented programming language. Everything moves with the help of the object (s). The class serves as a blue font with which we can create one or more objects. Classes> method> execution unit.

Even if you just want to type your name, you will do it using a class in Java.

+1
source

I know this is an old question, but I think this question is important in order to clarify something. Let's look at these definitions:

Java is a class-based, general-purpose programming language, object-oriented, and designed to have as few implementation dependencies as possible.

C is a universal imperative computer programming language that supports structured programming, the scope of lexical variables and recursion, and a static type system prevents unintended operations.

Python is an interpreted, high-level, general-purpose programming language. It supports several programming paradigms, including procedural, object-oriented, and functional programming.

What is a programming paradigm?

Well, is it a programming style or just thinking about software development. This concept does not apply to a specific language, but to a method of programming, a methodology.

Now let's dive into the programming paradigms we found in the following definitions:

Python supports procedural programming (PP). PP is a programming paradigm based on structured programming based on the concept of a procedure call. PP is also known as embedded programming, which uses a top-down approach. Thus, this paradigm is to write a list of instructions that tell the computer what to do step by step. It is based on procedures, which are also known as routines, routines or functions, simply contain a series of computational steps that must be completed.

Java only supports Object Oriented Programming (OOP). The focus is on encapsulating data and behavior in objects. The OOP application will use a collection of objects that know how to perform certain actions and how to interact with other elements of the application. For example, an object may be human. This person will have a name (this will be a property of the object), and he will know how to walk (this will be a method). A method in OOP can be considered as a procedure in PP, but here it belongs to a specific object. Another important aspect of OOP is classes. A class can be considered as a project for an object.

C supports Structural Programming (SP). SP, sometimes called modular programming, is a method developed to improve the reliability and clarity of programs. In SP, program flow control is limited to three structures, such as sequences, selections ( IF / THEN / ELSE ), and repetitions ( FOR / WHILE ), or a structure derived from a combination of the main three. The result is a program built from modules that are very independent of each other. This provides the logical structure of a written program to make it more efficient, as well as easier to understand and change.

What does it mean?

As follows from this simple definition, Java is a programming language based on classes and objects . Therefore, when you asked: โ€œCan I just create the program directly, how do I do it in other languages?โ€, The answer was NO, because it is part of the definition of the language.

Why can C and Python do this?

Well, this is because C * and ** Python support non-member functions, while Java only supports member functions.

What is the difference between a member function and a non-member function?

In fact, a function that is not a member is always defined outside the class. Although a member function should be defined as part of the class. This means that this function is a member of a particular class. Another difference between member functions and non-functions is how they are called (or called). To call a member function, you need a class object to access the function, but non-member functions can be called directly.

TL DR

Java needs a class to define a member function, such as main() because it only supports OOP. So, if you need a very minimal program, you can create a class using the main() method. Consider the following code segment:

 public class MainClass { public static void main(String[] args) { System.out.print("Hello World!"); } } 

This is the equivalent in C:

 #include <stdio.h> int main() { printf("Hello, World!"); return 0; } 
0
source

All Articles