Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Harnessing the Power of Objects Constants

Neros E.
Neros E.
528 Points

Does a dot operator assign value?

I’m a little confused as to what the dot operator does when it’s calling variables or classes instead of methods.

        class PezDispenser{
        public static final int MAX_PEZ = 12;
        private String mCharacterName;
           …….}

        PezDispenser pd = new PezDispenser;
        Pd.MAX_PEZ // This is where I get confused
        $2====>12  // Note that I did this in jshell

        PezDispenser.MAX_PEZ  
        $3=====> 12

I know it’s giving me access to the public constant variable MAX_PEZ, but said variable isn’t a method which can enact action, so what exactly is the operator doing for jshell to give me 12 as a result? It can't be that it's only showing me something I already know. I thought that maybe it's transferring the value of MAX_PEZ to the newly created object and/or class like some kind of assignment?

Note: I’m watching a video on the topic of constants and the point of the whole code is to permanently specify the number of pez that goes into a pezdispenser.

1 Answer

andren
andren
28,538 Points

The dot operator simply tells Java that you want to access something that exists within the object/class, nothing more.

So Pd.MAX_PEZ is simply telling Java to access the MAX_PEZ variable that exists within the Pd object. And as happens when you reference any variable by name it will simply return the value stored within it, nothing more.

Classes are blueprints that describe how an object should look, so all methods and variables defined within a class will automatically exist within all of the objects created from it, all of that copying happens as part of the objects creation.

Though it's worth noting that normally a variable/method can only be called from an object, not from the class itself. And each object will have an independent copy of their variables. This is not the case with MAX_PEZ because it is declared as static. Static variables and methods belong directly to the class and only one shared copy of them exists, it can therefore be called directly from the class itself.

By defining the max number of pez in a static constant you will end up being able to safely reference it both within the class and outside of it. The purpose of that is that if you write a bunch of code that relies on knowing that number, and then later on in development decide that you want to change what that number should be, you don't have to manually search through your code to replace the number. You just have to change the number initially assigned to MAX_PEZ and the rest of the program will still work just fine.

That is not that a big a deal in a small project like the one being built in this course, but in larger real word projects it would matter a lot, and creating variables to hold important values is quite common exactly for that reason.

Neros E.
Neros E.
528 Points

so IT IS simply telling me (accessing) something I already know, I guess I tried to make it more complicated than it was.

Also Andren thank you greatly for taking from your time to give a detailed and precise answer for this stranger! I was literally stuck on this for days and didn't dare go to next lesson without understanding something so basic, your post (out of a lot of them on the internet) finally made it click for me. Thanks again.

andren
andren
28,538 Points

I'm really glad to hear that :smile:. To be honest I was actually worried that my answer was a bit too wordy/ranty and strongly considered not posting it at all.

But it's good to see that I helped clear up some stuff for you, classes are one of the more complex parts of Java so you shouldn't feel bad about getting stuck on some parts of how they work. They are also something you'll see a lot of as you code Java though, so you'll get plenty of practice with them.

That was how I personally learned to understand Java and classes. After looking at enough examples and practicing a bit on my own it suddenly clicked with me, but before that I felt quite confused as well so I can certainly understand where you are coming from.

If you ever feel confused about something like this again, then feel free to ask me more questions. I'll certainly try to answer anything I can.