Is it possible to return a variable from an object-oriented class without using a function?

3

I do not know if this possibility exists. Here is a code to help with the question:

public class Square
{
    int side;

    public Square(int side)
    {
        this.side = side;
    }

    int getArea()
    {
        return side * side;
    }

    public static void main(String[] args)
    {
        Square square = new Square(5);

        System.out.println("Tamanho do lado: " + square + " / Area: " + square.getArea());
    }

}

Output:

  

Size of the side: colecoes.Square@15db9742 / Area: 25

Is it possible to exit this way without using a get function?

  

Size of the side: 5 / Area: 25

    
asked by anonymous 18.10.2016 / 16:59

2 answers

4

Implement in your class:

public String toString() {
    return side;
}

The rest of the code can stay as it is.

What happens is that the toString() method is called to convert your object to string when you concatenate it with other values in the System.out.println method. Since you have not yet made an implementation of the method, the toString of the parent class is called, and what it does is show the class name plus the reference to the object (which is the output you are seeing on the console).

Maybe this solution is not a good idea in the sense of expressiveness of the code, but will do exactly what you are looking for. There must be a better solution, but then you need to explain your intent.

Edit:

/ em> this way is a correct implementation.

And is there another way to represent an object by one of its attributes without having to explicitly invoke this attribute or its getter ? No , there is no Java.

Examples of this feature in other languages would be the default property of VB and the overhead of C # operators, but Java does not have this feature. Java's own types (String, Integer, Long ...) do this using compiler and VM features, not language features. That is: you can not achieve the same result in a class of your own.

    
18.10.2016 / 17:22
4

Yes it is possible:

class Square {
    public int side;
    public int area;
    public Square(int side) {
        this.side = side;
        area = side * side;
    }

    public static void main(String[] args) {
        Square square = new Square(5);
        System.out.println("Tamanho do lado: " + square.side + " / Area: " + square.area);
    }
}

See running on ideone and on Coding Ground >.

The object-oriented class can not have methods or functions. They may have public attributes as well. In that case you can get the value there is without a method. This is what I did with the area attribute.

This has a difficulty, nothing serious, it may be what you really want. Whenever the value of the side changes for some reason, including the construction there, this attribute needs to be updated so that whenever someone picks it up it has the correct value.

Note that the other side attribute used in the class does not have a visibility modifier, the ideal is always to make explicit. If you want it to be private (only the class can access it) declare as private . If you want any part of the application to access its value declare as public , as I did in the area attribute. In case I put public to give access, as it seems to be what you want, although the example is not correct.

In the moment of requesting the side, I did not use the object as a whole, I used this attribute square.side .

Some people find it wrong to access attributes directly without having a method to give indirect access. It depends on the application it makes sense to do this. But there are cases that is an exaggeration and using an extra method only serves to disrupt. See more in Vs Variable Property (it's C #, but the use of getter and < in> setter is equal).

Ideally the main() method should be in another class.

Although I have some recommendations for using toString() I would not do this to return the side. Having the object printed and displaying only the information on the side of it does not seem appropriate. Even if it is in this example, changing the composition of the class will give a problem. You have to conceptualize things right and implement them according to the concept, otherwise a maintenance would make the code no longer appropriate.

Trying to exemplify by taking the example in the comment in Caffé's response:

If you create a exemplo variable of type String . If you print only% w / o%, it will print the text "contained" in the variable. That is, when you print the variable you want it to print exactly the string . As the text is represented internally it does not matter. In this case what is being printed is conceptually correct.

If you have a exemplo variable of type square , that is, you have a square in it, if you print Square , you are expected to print the square, whatever that means. If you print only the side of the square is a wrong representation of what the square is. There you will use it waiting to print the side, one day needs to change and everything that existed does not work anymore. This problem occurs because it is conceptually wrong.

In addition, the question talks about not using a function, square is a function (method using correct terminology).

    
18.10.2016 / 17:16