Forum Stats

  • 3,727,659 Users
  • 2,245,434 Discussions
  • 7,852,908 Comments

Discussions

In Java, is a Constructor also a Method?

23bdeef9-6541-4b66-b2dd-61c3c308bbbb
edited October 2017 in New To Java

In Java, is a Constructor also a Method?

Looking at Oracle's definition of a Method it states that it needs the 6 components, including a return type. https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Additionally Oracle's definition of a Constructor states in the second sentence that they "look like methods", doesn't state that they are. https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

I've been told both throughout my time using Java and school. I'm not a beginner to say in Java, but this question should be an essential to the language when it comes to documentation/specifications.

I'm just looking for technical information to back either or.

eudriscabrera-JavaNet

Answers

  • morgalr
    morgalr Member Posts: 457
    edited October 2017

    No, and yes.  Here is a very simple way of looking at it that will help illuminate the differences:

    methods have the signature:

    <public> <static> [void | return type] MethodName(<parameter list>)

    Constructors have the signature:

    <public> <static> ClassName(<parameter list>)

    very similar to a method, but not.  Constructors are also called during the instantiation of your class to tell Java how to build your Object., your methods need to be called after instantiation of the Object from your class.

  • Unknown
    edited October 2017
    In Java, is a Constructor also a Method?

    No.

    I've been told both throughout my time using Java and school.

    Huh? What, exactly, have you 'been told'?

    but this question should be an essential to the language when it comes to documentation/specifications.

    And so it is.

    I'm just looking for technical information to back either or.

    Have you ever heard the expression 'be careful what you ask for'?

    The 'bible' for technical info about Java is The Java Language Specification

    https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html

    I suggest you fix a REALLY BIG cup of your favorite 'java' before sitting down to read it.

    Just keep searching for the word 'constructor' and you will find all the info you could ever want. Here are just a few quotes relevant to what you ask:

    Constructors (§8.8) are similar to methods, but cannot be invoked directly by a method call; they are used to initialize new class instances. Like methods, they may be overloaded (§8.8.8).

    'similar to methods'.

    The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs.  In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5). Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding. 

    See the last above? Constructors are NOT members - methods ARE members.

    Constructors are never invoked by method invocation expressions (§15.12).

    'never invoked' - but methods ARE invoked.

    The formal parameters and type parameters of a constructor are identical in syntax and semantics to those of a method (§8.4.1).

    'identical in systax'

    Unlike methods, a constructor cannot be abstract, static, final, native, strictfp, or synchronized:

    There is an entire list of reasons why right below that statement

    Except for the possibility of explicit constructor invocations, the body of a constructor is like the body of a method (§8.4.7).

    more 'similar'

    If that isn't enough reading you could also browse the Java Virtual Machine Specification

    https://docs.oracle.com/javase/specs/jvms/se7/html/index.html

    That way you can learn about all of the pseudo/intermediate code (p-code) instructions that compilers can generate.

    That document also defines/explains the Class file format.

    Happy reading!

    eudriscabrera-JavaNet
  • Unknown
    edited October 2017
    your methods need to be called after instantiation of the Object from your class.

    Methods can be, and often are, called DURING instantiation.

This discussion has been closed.