Hi could someone please explain to me why you have to create an instance before calling a non static method to the main function in java? What is the reasoning behind this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Because, they are instance members,to access them you need instance.
So now your second question about
static
Understanding Instance and Class Members
Static methods are class-level methods, so no instance is required.
Non-static methods are instance methods. Hence an instance is required.
Without this, object-oriented programming would really be no different from traditional procedural programming. When you execute a non-static function, you can access all the variables belonging to the object.
That being said, look carefully at the functions you're calling to see if they can remain
static
. Astatic
function is more portable, and less likely to cause side-effects.All the Static things of a class always belongs to a class and they are treated as a properties of the class .That's why they can be called by their name in that class, and called outside the class with the class name.
All the Non-Static things of a class always belongs to an object, they are always treated as properties of an object. That's why they can only be called after creating an object by a (.) dot operator.