I need help with UML Class Diagram

I am in an intro to Java course, and I need to provide a class diagram with my program assignment. I've got the class diagram completed in the way my professor wants it, but my question has to do with access specifications. I have a class for the main program, then a class below it. The main program creates an instance of the lower class. In the lower class I have made all of the fields private and the methods public. In the main class, however, I have never indicated private or public when declaring the variables. If this isn't specified in the source code are the variables public by default? And how do I show these fields in my class diagram (with a '+' for public, a "-" for private, or no access specification at all)?

Thanks in advance for any assistance that anyone may be able to provide!


  • Without saying public, private, or protected, your attributes, classes, methods, and constructors default to "package public". In package public, the item is accessible to every class in the same package and to nothing outside the package. For example, define a class A in package packagea without writing (public, private..). Define a class B in packageb. The class B can not make any direct reference to the class A. B can't even declare a variable of type A.

    Your question seems to be: "How do I represent package public access modifiers in a UML class diagram?" I'm pretty sure + would be the best option since UML doesn't really have a "package public" equivalent.

    I suggest that you make those attributes private, though. Making attributes private and get/set methods for them public is a generally recommended programming practice. This will also help to make your code get accurate representation in the UML class diagram using + and -.

  • Thank you for you assistance. This is an intro class, and up until the last section where we covered classes, all of our fields were left without a designation. This chapter, however, did say that it is usually a good idea to make fields private and methods public. So I guess that is what I'll do from here on out, and I'll designate them with a + in the UML Class Diagram.

    Thanks again for your quick response. This forum is a great resource, and I really appreciate it!
  • Yes, the forum is good especially when people answer questions quickly. There are serious technical problems that go ignored, though. I always get "forum error" messages every time I respond. It is annoying that the problems aren't fixed in the website since they are most likely very simple and annoys the people like me who freely answer questions on here.

    This isn't really for you, the UML diagram asker, but the people who may manage the site. The reason I mention here is that I've tried contacting the site administrators but nothing is leading to a fix in the site.
  • Yes, I've noticed that, too. Whenever I click on "Post Reply," the page just seems to cycle and nothing ever happens. My comments are obviously getting posted, but it is frustrating. I hope someone can fix it.

    Thanks again for your help!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!