A constructor is something that is used to initialize the instances of classes. Using a constructor you can create new objects often with parameters specifying the initial state or other important information about the object.
The difference between the two codes is in the first code you thought you are writing a constructor method but in actuality what you did is you created an object from a method with no return type. A constructor name must be the class name.
In the second code, you created a static reference to the non-static method function() which would throw an error and it does not have a constructor defined by you but the JVM will assign a default constructor to it.
Want to learn Java? Check out the Java certification from Intellipaat.