Java Programming: 14 – Constants

how to declare a constant in java This is a topic that many people are looking for. cfiva.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, cfiva.org would like to introduce to you Java Programming: 14 – Constants. Following along are instructions in the video below:
This tutorial. Were going to cover constants in java so to get started and and lets right click. Our tutorials basic package select new class and name the constants and click finish.
Im going to create our main method by typing main pressing ctrl spacebar and hitting enter. So all a constant is is a variable that can be changed once its value has been assigned constants can be handy. When you have a piece of data that will never change and you need to use it in numerous places.
A good example of a constant that java has available for us is the value of pi and we can see that by doing a system out and saying. Math dot pi and we run the. Application well see the value 314.
And so on and if you look at how were accessing that variable. Were accessing it through class called math. Were not accessing it through a reference to math.
But math itself so that means that the pi variable is a static member also notice that pi is capitalized whereas you would usually write variable names in lower camelcase. This is a convention when youre dealing with constant values. So what do we know so far.
We know constants never change once a value is assigned we know that at least judging by the example. Weve seen they are usually static. And we know that by convention constants are uppercase so how do we create our own constants.
Well lets go outside of the main method inside of the constants class and lets say were making a video game and our video game has a fixed resolution in other words. The width and height of the screen will never change and because theyll never change. Theyre good candidates to be constants.
So we could say something like int width. All in capital letters to follow the convention and well set it equal to 800 and we could do the same for height and well set it to 600 now right now these two variables are not constants. And by that i mean if we were to try to assign a different value to width or height later on in code.
We could do that just fine. What we need to do is to add a modifier to these variables. That tells java that these values should never change.
And that modifier is called final so we say final int width and final in tight so now our width and height variables can never change values. But theres a flaw with how weve coded this right now width and height are both instance variables. Which means every time.
We create a new constants object. Theres going to be different copies of width and height in memory which doesnt make much sense because theyre always going to be the same value so. We should do is make them static.
So we say static final int. With and static final and height and now no matter how many constant objects. We create there will only ever be one width and one height in memory.
But we could even go a step further and make these variables public now in the past. Ive said you typically want everything to be private and use getters and setters. But constants are an exception because you can never change their value.
So there is no harm in accessing them directly. So now we can say public static final input and public static final in tight and now you can use these values anywhere in your program. Just like we use map pi so for example we could say a system out and then constants dot width.
And we run the application youll see that 800 is printed out now while were inside of the constants class itself. We dont need to fully qualify. It with the class name so we could just get away with saying width.
Now recall that int is a value type and when we make a variable constant by using the final keyword. Were saying that the value assigned to that variable is final. It will never change now its very similar with a reference type for example.
If we were to say public static final person p1 equals new person. What this says is that our p1 variable. Which should be capitalized is a final reference it means the value in p1.
Which is a reference is final. And what that means is that you can never say p1 equals. A different person.
But you can access and change. The data of the p1 object. It refers to all that is final is the reference itself not necessarily the data inside the object that the reference points to so lets add to our comment and say declare constants by using the final modifier and constants can be public.
Because theres no harm in accessing. A variable directly if you cant alter. It now theres one last thing.
I should mention about the naming convention. Lets say. I had a public static final int variable.
And i wanted it to count the number of enemies in our game. If you have a constant and you want to name it with multiple words. The convention is to use all uppercase and an underscore to separate the words so we can say num enemies equals.
5. So now anytime youre reading code. And you see variables that are all uppercase.
Youll know that those are constants and remember that because constants are usually static. You should access them through the class name directly and not through an instance of the class that should be just about everything you need to know about constants for now thanks for watching. .

how to declare a constant in java-0
how to declare a constant in java-0

Thank you for watching all the articles on the topic Java Programming: 14 – Constants. All shares of cfiva.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment

Your email address will not be published. Required fields are marked *