1

Is there any way to directly accept values for an object in Java?

In Dart this is how I would do it.

class Nice
{
String hello;
Nice(this.hello);
}

Is there some similar way in Java?

4 Answers4

3

You should take in consideration de OOP Concept "ENCAPSULATION" This concept is also often used to hide the internal representation, or state, of an object from the outside. This is called information hiding. The general idea of this mechanism is simple. If you have an attribute that is not visible from the outside of an object, and bundle it with methods that provide read or write access to it, then you can hide specific information and control access to the internal state of the object.

class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

The main idea is that the member variable is private and can be accessed or modified through its GET and SET method.

This is possible in JAVA but you disrespect the concept presented above. What is happening here is that the member data has its "Access Modifier" as public and its value can be accessed.

public class Main {

    public static void main(String[] args) {
        Person person = new Person();
        person.name = "Person1";
    }


}
class Person {
    public String name;
}

Visit

Andres P
  • 56
  • 1
  • 6
  • @NephewofStackoverflow there are [ways](https://openjdk.java.net/jeps/0) to contribute to the evolution of java, but I doubt the community will accept reatroactivly changing java 8 to accommodate your ideas. – Federico klez Culloca Feb 04 '20 at 15:55
  • @NephewofStackoverflow this is neither Twitter nor your blog. Please keep the comments in topic and to the point. – Federico klez Culloca Feb 04 '20 at 16:11
  • 1
    @NephewofStackoverflow It seems like you don't understand how to [program to an interface](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface/384067). But anyway, Stack Overflow is not a place to rant about all the things you don't like about Java. – Sweeper Feb 04 '20 at 16:11
  • Sorry for the inconvenience – Nephew of Stackoverflow Feb 04 '20 at 16:12
  • Sure, you can modify the JDK as much as you want and try to publish your changes, but don't expect others to *like* them and follow you. You're basically forking Java if you don't go with the established way to do such things (the JEPs I linked in my other comment). – Federico klez Culloca Feb 04 '20 at 16:23
  • Is there some way to contact the main developers? Maybe I can explain them my idea, so if they are convinced I can work on it further and then they can present it to the developer community, so if they're convinced with the idea, they can go foward to implement it. – Nephew of Stackoverflow Feb 04 '20 at 16:31
  • (This comment existed 2 comments prior the current comment) So in conclusion, Java doesn't support any such feature for the time being and there's no way to integrate any such feature directly in JDK 8. (Note: I am not de-constructively criticizing Java, but it would be great if there were a way to do it or if atleast I or anyone else could do something about it) – Nephew of Stackoverflow Feb 04 '20 at 16:33
  • @NephewofStackoverflow don't take this the wrong way: why do you think you're better than anyone else and don't have to go through the community process like everybody else? There's a reason there's a process to introduce features to the language. If you don't understand that (especially in the case of Java), you're probably too inexperienced to give these suggestions anyway. Again, don't take this the wrong way, I'm not trying to put you down, just to give you a bit of perspective on the matter. – Federico klez Culloca Feb 04 '20 at 19:19
1

You can declare the field as public, then it is accessible from the outside:

public class A {
    public String myField;
}

public class Runner {
    public static void main(String[] args) {
        A a = new A();
        a.myField = "my cool value";
    }
}

However, this is not the "java-way" of doing it, since it breaks encapsulation.

Seb
  • 1,721
  • 1
  • 17
  • 30
1

If you directly want to capture values for objects when they are initialized then make a constructor like:

    class Nice {
        String hello;
        String greetings;

        Nice(String hello, String greetings) {
            this.hello = hello;
            this.greetings = greetings;
        }
    }

Then create object by passing values directly like:

    Nice nice = new Nice("hello", "Good Evening!");
Md. Yamin Mollah
  • 1,609
  • 13
  • 26
0

The Java equivalent of that Dart class would be

class Nice {
  private String hello;
  public Nice(String hello) {
    this.hello = hello;
  }
  public String getHello() { 
    return hello;
  }
  public void setHello(String hello) {
    this.hello = hello;
  }
}

It is no more or less efficient than the Dart class.

There is no inherent performance efficiency in the way the Dart class is written that a Java compiler can't get out of the corresponding Java code. The Dart code is syntactically shorter, but that is all.

lrn
  • 64,680
  • 7
  • 105
  • 121
  • So doesn't that save statically allocated space for `hello` before assigning it to `this.hello` – Nephew of Stackoverflow Feb 04 '20 at 17:46
  • I don't see why it should. Depending on the compiler/runtime implementation, function/constrcutor calls might pass arguments in CPU registers or on the stack, then the constructor implementation moves that value into a memory slot in the newly allocated object. The Java compiler can easily be as efficient as the Dart compiler in doing this. Or maybe it inlines the entire allocation and initialization. – lrn Feb 04 '20 at 17:51
  • So 4/8 bytes of memory are saved. Ain't it? – Nephew of Stackoverflow Feb 04 '20 at 17:57
  • I just wanted to know if Java offers some way that's similar to that of Dart. But apparently it seems that it doesn't offer any such feature. – Nephew of Stackoverflow Feb 04 '20 at 17:57
  • A boolean variable in Java might take (4-8)+1 bytes in Java. So I was just looking for optimal ways to write code without hogging in much memory in a clean and easy to understand way. – Nephew of Stackoverflow Feb 04 '20 at 17:59
  • There is nothing in the syntax here which saves memory. The parameter will be passed by the caller, likely on the stack or in a register, and the constructor code will then move it from there into its place in the object which was allocated in the heap. Both languages will do that. Dart does not magically store the value of the constructor argument directly into the new object's memory - not unless the compiler does something clever which corresponds to inlining the constructor, and then the Java compiler can do exactly the same thing too. – lrn Feb 04 '20 at 19:40
  • But by specifying a variable name aren't we allocating more memory for the local variable? Like doesn't Java assume that more operations might be performed on parameter-x? – Nephew of Stackoverflow Feb 07 '20 at 18:29
  • No. A *parameter* name refers to the parameter variable. It's quite possible (and the default implementatation strategy in languages like C) to make that variable be backed by the stack slot that was used to pass the argument. Java can easily do the same. In practice, the compiler can be much more complicated than that, but you can be certain it's not being less efficient. – lrn Feb 07 '20 at 22:18
  • Lol, I don't even know why are we still discussing this But anyways...Thanks a lot for your time. According to me, Java is a lot more complicated than just that, but coming from a Dart background I just wanted to code effectively in the Dart way of doing things. – Nephew of Stackoverflow Feb 08 '20 at 08:42