Java, Class Members

Tram Ho

Instance & Static

The components that can be present in the code that define a class in Java are also divided into two groups just like for JavaScript and other programming languages ​​that support OOP :

  • Instance Membersproperty properties and method methods belong to object created from class , also known as instance .
  • Static Membersproperty properties and method methods do not belong to instance but to object representing that class , and are referenced from the class name.

Overloading Methods

Compared to JavaScript , Java ‘s class definition syntax is mostly similar, but there are also a few differences:

  • Initializers are methods without the constructor keyword.
  • There can exist different definition versions of the same method name method name .

The first difference is probably due to the original language design, JavaScript ‘s constructor keyword helps initializers to be better marked apart when placed next to the definition code of other methods. Hopefully Java will update a few of these little features to make it even more perfect.

The second difference is due to Java ‘s type characteristic of static-typing . Static-styled languages ​​have always tried to support this feature to make it possible for coders to reuse function names. There are many cases when we want to have multiple definition versions of the same function name, because the general function of the function is the same, but for different input parameter sets, there will be different processing logic. little bit. The operation of redefining a function name multiple times with different signature sets is called overloading methods .

In the example code above, we have an initializer overload with an instance with no parameters and an instance with data parameters.

A signature set represents a version of method definition that includes: the return data type, the method name ‘s name, and the input parameters’ data types. Since Java initializers don’t have names, we are temporarily having the same signature sets as above.

Share the news now

Source : Viblo