Reputation: 343
I am starting to learn the Java programming language and I am a little confused.
I would like to create a simple program like adding 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 a main class and call my program from it.
I use both Netbeans and IntelliJ.
Can I just create the program directly the way I do it in other languages? I mean is the concept of a class necessary in Java?
Upvotes: 8
Views: 4957
Reputation: 8996
I know this is an old question but I think the question is important to clarify something. Let's see this definitions:
Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible.
C is a general-purpose, imperative computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations.
Python is an interpreted, high-level, general-purpose programming language. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Well my young Padawan, a programming paradigm it is a style of programming, or just a way of thinking about software construction. This concept does not refer to a specific language, but rather to a way to program, a methodology.
Now let’s dive into the programming paradigms that we found on this definitions:
C supports Structured programming (SP). SP sometimes known as modular programming, this is a technique devised to improve the reliability and clarity of programs. In SP, the program flow control is restricted to three structures (i.e., sequences, selection [IF
/THEN
/ELSE
], and repetitions [FOR
/WHILE
]), or to a structure derivable from a combination of those. The result is a program built of modules that are highly independent of each other. This enforces a logical structure on the program being written to make it more efficient and easier to understand and modify.
Python supports Procedural Programming (PP). PP is a programming paradigm, derived from SP, based upon the concept of the procedure call. A procedure call is a simple statement made by stating the procedure name, and listing actual parameter names or values within parentheses. So this paradigm is about writing a list of instructions to tell the computer what to do step by step. It relies on procedures, witch simply contain a series of computational steps to be carried out.
Java only supports object-oriented programming (OOP). This main focus is about encapsulating data and behavior into objects. An OOP application will use a collection of objects which knows how to perform certain actions and how to interact with other elements of the application. For instance, an object could be a person. That person would have a name (that would be a property of the object), and would know how to walk (that would 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 are classes. A class can be considered as a blueprint for an object.
As this simple definition says, Java is a class-based and object-oriented programming language. So when the user asked: "Can I just create the program directly the way I do it in other languages?", the answer is NO, because it is not part of the definition of the language.
Well, it is because *C and Python support non-member functions, while Java supports only member functions.
Basically, a non-member function always is defined out of a class. While a member function must defined as part of a class. This is to identify that that function is a member of a particular class. Another difference between member functions and non-member functions is how they are called (or invoked). To call a member function you need a class object to get access to the function, but non-member functions can be invoked directly.
Java needs a class to define a member function like main()
because it only supports OOP. So, if you want a very minimal program, you can create a class with a main()
method. Consider the following segment of code:
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;
}
Upvotes: 3
Reputation: 1583
Java requires every function/method to be defined in a class. That includes the main method.
The restriction is not imposed by all object-oriented languages. In some cases the constraint is lifted merely as a convenience (i.e. Python, Ruby). Some languages, like JavaScript and Lua, provide OOP features through a prototype-based mechanism. Java enforces OOP with a class system, so you may hear it referred to as a class-oriented language.
Upvotes: 3
Reputation: 9786
While you do have to create a class, the question specifically asks if you have to create a class for your program and a main class to call it. The answer to that is "no".
You can create a single class with a main method and have your logic inside that if you want a very minimal program. Something like this:
public class MyClass {
public static void main(String[] args) {
// Do Something here
}
}
Upvotes: 2
Reputation: 1552
Java is a Object Oriented programming language. Everything is driven by object(s). A class serves as an blue-print, using which we can create one or more objects. Class > method > execution unit.
Even if you want to just print your name - you will do this using a Class in Java.
Upvotes: 1
Reputation: 1503649
Can I just create the program directly the way I do it in other languages?
No.
I mean is the concept of class necessary in Java?
Yes. Every method, field etc is always in a class (or interface). Yes, that's an overhead for tiny programs - but for larger programs, the impact is pretty tiny.
As ever, use the right tool for the job - if you want a script of a few lines, use a scripting language. If you want more structure and organization, then you should expect a bit of "ceremony" to go with that.
Upvotes: 16