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);
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
The object-oriented class may or may not have methods and not functions. They may have public fields as well. In this case you can take the value exists of it without a method. That’s what I did with the attribute area
.
This has a difficulty, nothing serious, it can be what you really want. Whenever the value of the side changes for some reason, including the construction, this attribute needs to be updated so that whenever someone picks it has the correct value.
Note that the other attribute side
used in the class does not have a visibility modifier, the ideal is always explicit. If you want it to be private (only the class can access it) state how private
. If you want any part of the application to access its value state as public
, as I did in the attribute area
. In the case I put public to give access, as seems to be what you want, although the example is not correct.
In use at the time of asking for the side, I did not use the object as a whole, I used this attribute square.side
.
Some people consider it wrong to access attributes directly without having a method to give indirect access. It depends on the application it makes sense to do just that. But there are cases that is an exaggeration and use an extra method only serves to disturb. See more in Property Vs variables (is C#, but the use of getter and Setter is equal).
Ideally the method main()
should be in another class.
Although there are some recommendations for using the toString()
i would not do that to return the side. Having the object printed and showing only the information on its side does not seem appropriate. Even if it is in this example, changing the composition of the class will be a problem. Have to conceptualize things right and implement according to the concept, otherwise a maintenance would make the code not be more suitable.
Trying to exemplify by taking the example in the comment in Caffé’s answer:
If you create a variable exemplo
of the kind String
. If only print exemplo
, it will print the "contained" text in the variable. That is, when you print the variable you want it to print exactly the string. How the text is represented internally does not matter. In this case what is being printed is conceptually correct.
If you have a variable square
of the kind Square
, I mean, there’s a square in it, if you print it square
, You’re supposed to print the square, whatever that means. If you only print the side of the square, it is a wrong representation of what the square is. Then you’ll use it hoping it prints the side, one day it needs to change and everything that existed doesn’t work anymore. This problem occurs because it is conceptually wrong.
In addition, the question talks about not using a function, the toString()
is a function (method using correct terminology).
Overwrite the method
toString
classSquare
and implement to return a string the way you want to display.– Renan Gomes
@but in this case is returning the same way, right?
– Maniero