1

The following code does not work because the compiler says that the function AnotherNewClass() does not exist? Why is that? Isnt a constructor just a function? Why cant a constructor function without a reference to a particular object?

class AnotherNewClass
{

    public AnotherNewClass(){
        System.out.println("Hello World!!");
    }

    public AnotherNewClass(String arg){
        System.out.println("Hello World!!");
    }


    public static void main(String []args){
        AnotherNewClass("Hello World!!");//This is the offending code; where the compiler throws an error
    }

}

PS. From a few comments below I would like to clarify that I understand that I am not using the new keyword, the purpose of this question was to highlight the difference between a function and a constructor(which cannot be called WITHOUT 'new')

Deepak
  • 341
  • 4
  • 15

7 Answers7

3

It can, but you will need the new keyword.

new AnotherNewClass("Hello World!!");

Note that this does more than just calling the constructor. That is exactly why you need the new keyword. The new keyword will allocate memory on the heap where an object of your class can fit in. But that memory doesn't contain such an object yet. Then the constructor initializes that piece of memory. That is why you do not want to use constructors as regular functions.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
2

The purpose of having constructors is to initialize Objects in Java

Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new.

so the answer to your question according to me is, they can't be because they are not supposed to be, that is how it is designed. you can further read what happens when we say new Class() in depth, you will come to know the actual reason.

dev2d
  • 4,245
  • 3
  • 31
  • 54
0

1 - Constructors are used to instantiate classes, and here you are doing it wrong. It should be

new AnotherNewClass("Hello World!!");

Take a look at this question for a good explanation.

2 - In Java, there we say methods and not functions.

Community
  • 1
  • 1
Naili
  • 1,564
  • 3
  • 20
  • 27
0

The answer about the reason could either become rather philosophical, or just refer to the Java Language Specification. In any case,

new AnotherNewClass("Hello World!!")

should work.

Marco13
  • 53,703
  • 9
  • 80
  • 159
0

I assume you're asking from a language point of view. In other words, why Java doesn't allow it as opposed to how to do it.

I think it's because a function (method) normally requires an instance of an object. A constructor is a special case because it creates this instance, which requires the new keyword. Constructors shouldn't be called at any other time for what I'd hope are obvious reasons!

In your main method above, you don't have an instance of AnotherNewClass on which to invoke the constructor, which is what the compiler is telling you.

ATG
  • 1,679
  • 14
  • 25
0

So this is what I have understood from the comments and answers below:

A constructor is inherently different from every other function in Java, in fact it is a function only in form and never in processing.

A constructor can only process a piece of memory assigned to it by the 'new' operator or any local memory. Whereas a function can process memory that a calling object may refer it to.

The 'new' operator assigns that piece of memory thus essentially 'creating' an object and the constructor 'fills' that piece of memory. THAT piece of memory (given to it by the 'new' operator) is the MAIN difference between a function and a constructor in terms of the processing they do.

Deepak
  • 341
  • 4
  • 15
0

​First of all you can only call static methods from the main method. And as others have said constructors has to be called with the new operator.

AnotherNewClass an = new AnotherNewClass("Hello World!!");

Once you have an instance you can call other methods as shown below.

an.someMethod();
fastcodejava
  • 39,895
  • 28
  • 133
  • 186