Create a constructor that takes arguments without 'new'

I want to create a class that works like a String, i.e. does not require new String("value");

.
For example:

public class Number { ... }

// main
Number num = 5;

      

Is it possible?

+3


source to share


3 answers


The short answer is no.

Long answer, you wouldn't want this behavior.

The great thing about strict OO language is that you can guarantee that an object is an object ... the problem with java is that it is a class based language and less OO language ... which causes such weirdness.

int myInt = 5; //primitive assignment.

      



It is a value, it is not an object, and does not conform to the standards for what an object represents in Java.

Integer myInt = new Integer(5);

      

creates a new object in memory by assigning a reference to it, and then any "passing" of that object is by reference.

There are many frameworks that can give you the visibility of this assignment, but new

lets you know that you are creating a completely new object, not just the value of some random piece of memory that is believed to be declared as a string of integer bits.

+4


source


As AnthonyJClink said, you can't. But .. you can (kinda) shorten your creation code by not using the keyword new

if you instantiate your instance in a static method of your class. For example:

public class MyClass {

    private MyClass(){
    }

    public static MyClass create(){
        return new MyClass();
    }

    public static void main(String[] args) {
        MyClass instance = MyClass.create(); // Add parameters as needed
    }
}

      



This reminds me of methods like int b = Integer.parseInt("444");

. In some cases, your code becomes more readable. It really depends on your needs. This publicly available static instantiation is also often used with single point methods getInstance()

.

+1


source


No, but if you're looking for syntactic alternatives, there are some constructor alternatives that have their own application.

Enums are not what you are looking for.

Static factory methods.

Rule x = Rules.def("abc");
Rule y = def("abc"); // Static import

public abstract class Rules { // Factory
    public static Rule def(String s):

      

(You can drop the class name not only from static import, but also from being inside a superclass.)

class Grammar { protected Rule def(String s) { ... } }

Grammar g = new Grammar() {{
     Rule x = def("abc");
}};

      

Builders, free API

Grammar g = GrammarBuilder.grammar("Pascal")
   .rule("S")
        .seq()
            .keyword("PROGRAM");
            .nont("Decl")
            .nont("Block")
            .endseq()
        .endrule()
   .rule("Decl")
        ...

      

Builders, free API

Grammar g = grammar("Pascal",
   rule("S",
        seq(
            .keyword("PROGRAM");
            .nont("Decl")
            .nont("Block")
            .endseq()
        )),
   rule("Decl",
        ...)
);

      

Java 8 lambdas assigning functional interfaces

Producer<String> x = () -> "beep";
x = () -> x.apply() + x.apply();
System.out.println(x.apply());

      

0


source







All Articles