简体   繁体   中英

Difference between a method with void and a constructor

What is the difference between using a method with void, and a constructor? For instance:

public class Time {
    public void getMethod() {

    }

    public Time() {

    }

}

Where do I use what? Thank you

  1. Constructor is called after creation of a new Object() . Typically to do some initialization, to prepare the object. Methods - you call them when you like.
  2. Constructor can be called only once on a certain object. Methods can be called many times.
  3. Constructor can't be static , because it would be not logical, static says "belongs to class, not an object". Methods can be static .

Constructors are methods that belong to a class that are associated with its creation. When you declare an object using Object a = new Object() ; This is where constructors are invoked.

You should use constructors to organize any data that you'll need for the rest of the class. For example, if you are making a Time class, the Time constructor might get the current time and set it in a variable to use later.

Other methods are simply that. They are the methods that do some calculations or work for the class. For example, you might have a method that accepts a date and returns the days between the date entered, and the current date.

'void' is simply the return type of the method.

Constructors do not have return types.

A constructor is used to create a new object. Its use leads to the return of a reference to a new object (although technically the constructor does not return the reference itself). As such void would be a meaningless keyword for a contructor. It is used as Object o=new Object(parametersIfApplicable)

On the other hand a method with a void return parameter returns nothing (but would usually change the internal data within the object (or less frequently change one of the objects passed to it); otherwise calling it would be pointless).

Summary

  • Calling a contructor leads to a new object and returns a reference to it
  • A (non static) method is called on an existing object to do some work; it returns nothing

A void method specifically does not return any data or object. Pragmatically, a constructor does not return anything.

From a hardware perspective, it initializes the information given at construction allocated by the object declaration by the caller; Doughty provides a broad overview under " References to and Creating Objects ".

In order to use the method, getMethod() , you must create an instance of the class Time which is done by constructing its object defined by its constructor.

Time currentTime; //Declaration of object only allocates space in memory for it Time currentTime; //Declaration of object only allocates space in memory for it currentTime = new Time() //Constructs an instance of Time
//and assigns the object reference to variable currentTime

Then using the method with return of void:

currentTime.getMethod();

Simply calls the method so it performs its tasks. It returns no data or object, but control back to where it was called.

Most of an object's methods will be called the same way when invoked by an object under construction as when invoked from anywhere else. Constructors are different. A constructor is a special sort of void method which cannot be called directly from "normal" code. Instead, all constructor calls must take one of two forms:

  • A call made at the very beginning of one constructor to a another constructor in either the present class or superclass.

  • A call generated by the system as part of the following sequence (pseudocode given for x = new Foo(123); ):

     // "Magical" system routine to create an object without running constructors Foo temp = InternalSystemMagic.CreateNewObject([foo's type]); temp.Foo_constructor(p1); // Now invoke the constructor x = temp; // Note that if the constructor throws, x will not get set.

Note that the compiler can produce code which will first create an object of type Foo without running the constructors, and then call a constructor on the newly-created type, even though neither operation would be legitimate without the other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM