-2

In many languages, we use new to instantiate a new instance of a class. For example, in Java:

class MyClass {
    int number = 1;

    MyClass(int n) {
        self.number = n;
    }
}
MyClass obj1 = new MyClass(5);

However, as I've been using Python more and more, I've come to wonder why the new keyword is even necessary. In Python, we can simply do:

class MyClass:
    def __init__(self, n):
        self.n = n
obj1 = MyClass(5)

So, what's the purpose of the keyword? Is there some syntactic ambiguity that we resolve by using the keyword?

Michael Kolber
  • 1,309
  • 1
  • 14
  • 23
  • 4
    Because that's how the language was designed. Because Java is based on C++ and that's how C++ was designed. – Andreas Jul 15 '19 at 16:43
  • 2
    @Andreas I agree that Java may have it because it is based on C++ having it, but then the question becomes "_why does C++ have it?_" – GrumpyCrouton Jul 15 '19 at 16:46
  • @Andreas Do you have evidence that that's the actual reason (e.g. some kind of reference or something)? – EJoshuaS - Stand with Ukraine Jul 15 '19 at 16:46
  • 2
    You can ask this question regarding every syntax design decision in every langauge. Why are there no braces in Python? Why are semicolons optional in Python/JavaScript? Most of the times the answer will be "just because". Other times it will be "because language X wanted to simplify langauge Y's syntax". – DeepSpace Jul 15 '19 at 16:48
  • 1
    Other times the answer will be "we don't know, you'll have to ask the language's designer", and hope they will remember an arbitrary decision from 30 years ago. – DeepSpace Jul 15 '19 at 16:49
  • @DeepSpace Presumably there's *some* reason, though. – EJoshuaS - Stand with Ukraine Jul 15 '19 at 16:49
  • "new" makes it more clear a new instance of an object is being created. "MyClass(5)" could be the name of a method. – trilogy Jul 15 '19 at 16:49
  • 1
    @DeepSpace Perhaps, but usually languages don't add unnecessary bloat for no reason. having `new` "just because" (where either having it or not having it would make no differences ever) would literally be pointless – GrumpyCrouton Jul 15 '19 at 16:49
  • 1
    @GrumpyCrouton Because in C++ memory allocation and release is *manual*, so it's very important to recognize when you allocate memory so you can remember to release it again. If memory was allocated implicitly, it would be too error-prone to memory leaks. Heck, even with the `new` keyword, C++ is too error-prone to memory leaks. – Andreas Jul 15 '19 at 16:51
  • 2
    @DeepSpace You can easily argue the converse, why don't Java and C++ have a `call` keyword that you use before ever calling a function? Generally the answer will be "Because it wasn't necessary, so why make the user do more work than necessary?" It seems that the fundamentals of a language's syntax generally follow *some* reasoning. – Michael Kolber Jul 15 '19 at 16:51
  • @Andreas That sounds a lot more explanatory than "just because". Actually seems like it may be a good answer – GrumpyCrouton Jul 15 '19 at 16:52
  • 2
    @GrumpyCrouton The "just because" for Java is that `new` is used *just because* is was used in C++. The question is about "why in Java?", so the answer is: Just because. For the question about Java, the reason C++ has it doesn't really matter. – Andreas Jul 15 '19 at 16:53
  • 1
    @Andreas I guess that is where we would have to agree to disagree. The question is not a vacuum, the answer for "why does Java use `new`" can easily be "because Java is based on C++, which uses `new` because it would be to error-prone to memory leaks otherwise" C++ and Java are closely related in this way, so it doesn't make sense to me why the reasoning in C++ couldn't be part of the answer, when that's likely "why Java has it". Or do you think "just because C++ does it" is a valid answer to the question, which would just lead someone to ask _another_ exact question with just C++ specified? – GrumpyCrouton Jul 15 '19 at 16:55
  • 1
    @GrumpyCrouton - For the same question about C++, I added an answer to the original question [Why do C# and Java bother with the "new" operator](https://stackoverflow.com/questions/433591). – Andy Thomas Jul 15 '19 at 17:17

1 Answers1

6

It's to explicitly say "I want to create a new instance of a class"

This is to differentiate from method calls, as you can even do this:

public class Main {
    public static void main(String[] args) {
        Foo();
        new Foo();
    }

    public static void Foo() {
        System.out.println("Method");
    }

    public static class Foo {
        public Foo() {
            System.out.println("Class");
        }
    }
}

Running main gives you

Method
Class
EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • So it's basically just to allow methods and subclasses to share a name? – Michael Kolber Jul 15 '19 at 16:44
  • 3
    Is it documented somewhere? or is it your "feeling"? Or just common sense tells you so? – Michał Krzywański Jul 15 '19 at 16:44
  • 1
    @michalk convention I guess. Java was heavily influenced by the C family, where memory management was explicit. It's a remnant from a time where "new" allowed users to more easily tell where memory was allocated – EDToaster Jul 15 '19 at 16:54