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).