I do not have any Java experience and have a C background.
I would like to create a new data type, like an abstract data type. In C, this would be done by creating a structure for the new data type; I understand that in Java, you create a class for that new data type, and then go about creating objects for that class.
I have written this two different ways, and both of them seem to work. But I do not understand the shortcomings of the second way.
Here, I create a class for the data type, create an instance of it, and then perform operations. I've found this to be the design used in tutorials on the Internet.
public class DesignOne {
public static void main(String[] args) {
MyDataType obj = new MyDataType(3,4);
System.out.println(obj.sum());
}
}
class MyDataType {
int i;
int j;
MyDataType(int i, int j) {
this.i = i;
this.j = j;
}
int sum() {
return this.i + this.j;
}
}
Here, I create an instance of the class in the same class where I have my driver function. Operations like sum
are also defined as members of the class. Basically, there's just one class.
public class DesignTwo {
int i;
int j;
DesignTwo(int i, int j) {
this.i = i;
this.j = j;
}
int sum() {
return this.i + this.j;
}
public static void main(String[] args) {
DesignTwo obj = new DesignTwo(3,4);
System.out.println("sum == "+obj.sum());
}
}
What are the shortcomings of DesignTwo, and why is DesignOne preferred?
Because that is how the language was designed. The JVM first loads the class containing the main function and then calls the main() method. Without loading the class, main() cannot be called, i.e, main() doesn't exist without its enclosing class. Save this answer.
Main method is the entry point of the execution in Java. When we execute a class JVM searches for the main method and execute the contents of it line by line. If you observe the following example you can compile a this program but if you try to execute it you will get an error saying “Main method not found”.
It is not necessary for all the classes to have a main method. main method is used as an entry point for java applications. So once you have entered the java code using main method of a single class you can call other classes code form there.
To compile a program, you doesn't really need a main method in your program. But, while execution JVM searches for the main method. In the Java the main method is the entry point Whenever you execute a program in Java JVM searches for the main method and starts executing from it.
It essentially boils down to Abstraction. You should try to give each class one role. Here, you have two roles that need to be filled:
Therefore you should use two classes. The advantage becomes more obvious once you start to want to use and display more than one type of thing in your program.
There's no inherent difference between the two (though non-final public fields are usually discouraged generally). The only difference is where you put the main method, which doesn't conceptually belong to any specific class. In larger programs it usually lives in a separate launcher class just because it doesn't make sense to tie it to some arbitrary class, but that's mostly irrelevant for data modeling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With