At my uni the software design teachers like to ask questions about UML diagrams, such as this:
In the following class diagram, can class
Bar
’sbar()
operation modifyBar
’sb
attribute? Can it modify classFoo
’sa
attribute?
Now as far as I know, static operations may only modify a class’s static attributes. Bar
’s b
is not static. Does this also apply to other classes’ attributes, such as Foo
’s a
?
But still, how about this?
class Bar {
public static void bar()
{
var bar = new Bar();
bar.b = something;
}
}
It seems possible to implement bar()
so that in its body it instantiates a Bar
object and then modify its non-static attribute. The same could be done with instantiating a Foo
and modifying its a
.
I know I should differentiate between an example Java implementation and the ‘abstract’ UML diagram, but I can’t help but think of what makes sense and can be implemented while looking at these diagrams.
So what is an intuitive way to think about this? Is there anything that ‘prevents’ me from implementing the code I provided in the diagram?