Question

In: Computer Science

BIG JAVA CH4: Understand the proper use of constants Question: What is the purpose of keyword...

BIG JAVA

CH4: Understand the proper use of constants

Question:

What is the purpose of keyword final? Explain what happens when you use it and give an example.

Solutions

Expert Solution

In Java, "final" is a keyword, used along with a variable. The main aim of final keyword is to create some sort of constraint, for the above.

In a variable, final is placed as a prefix, which, in a way, makes that variable's value constant. This means that oncea variable is "final", during it's declaration and initialization, it means that it's value is fixed, final and cannot be changed later on in it's lifetime.

If, in any part of our code, there is an attempt made to modify the value of this constant, it will give an compilation error, i.e. it cannot be modified. Let's take a look at an example that show how it is declared, instantiated and what happens if there's an attempt to modify it's value.

Example:-


public class Finalconst { //public class FInalConst created, containing constant
  
   final int mynum=5; //constant munum declared, using "final" keyword
  
   void changeval(int x)
   {
       mynum=x; //changeval functtion, attempts at changing value of constant number
   }
  
   void printval() //function to print the value of mynum
   {
       System.out.println(mynum+"\n");
   }
   public static void main(String args[])
  
   {
       Finalconst f=new Finalconst(); //Object o class Finalconst created
       f.printval(); //Printing the value of our constant
       f.changeval(9); //Changing the value of constant to 9
      
   }
  

}


Output:-


Related Solutions

what are the differences/similarity between the keyword "this" in java and the keyword "self" in python...
what are the differences/similarity between the keyword "this" in java and the keyword "self" in python and why or when should we use these two keywords? if you could provide a code example side by side that would be great! thank you!
Please use your own words, Don't use a picture. What is the purpose of keyword final?...
Please use your own words, Don't use a picture. What is the purpose of keyword final? Explain what happens when you use it and give an example.
What you understand from Big 5 & Personality Traits and explain it? with proper reference, please!
What you understand from Big 5 & Personality Traits and explain it? with proper reference, please!
what can proper phrasing of a question do?
what can proper phrasing of a question do?
Question Define big data and data mining. What purpose does collecting huge amounts of data serve?...
Question Define big data and data mining. What purpose does collecting huge amounts of data serve? Consider Twitter. Do you believe big data is accurate and reliable? Why or why not? What type(s) of sampling methods could be used with big data? What sampling errors could occur and how could they be avoided? Has collecting big data been helpful for businesses? Why or why not? What do you see happening in the future with big data?
Big Data raises many questions about security, ethical standards, and proper use of data in health...
Big Data raises many questions about security, ethical standards, and proper use of data in health information systems. Ensuring ethical standards means that rules have to be established, but also enforced. Explain the types of activities you would implement in an organization you manage to ensure the proper use of data, particularly in light of the concerns Big Data raises. What safeguards would you recommend for collecting and retrieving healthcare data?
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
. Big picture question: Now that you understand statistics. I want you to answer a couple...
. Big picture question: Now that you understand statistics. I want you to answer a couple of questions. Can a skilled statistician “make anything significant”? How would this be/not be accomplished? How could you design a study to guarantee significance? What is statistical power and how is it relevant to this big picture question? [6 Points]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT