Where are fields declared Where are local variables declared

Where are fields declared? Where are local variables declared? Where does the execution of a Java application start? What are set() methods used for? What are set() methods used for? Why do we declare fields of a class using private access? Suppose you have written a class named Student Also suppose the Student class you wrote has a constructor method that takes in as parameters the Last Name of the student and the 10 number of the student (800 number) Declare a reference variable for a Student. Name this reference variable: myStudent Create an instance of the Student class using your last name and your 800-number Assign the address of this instance to the variable you declared above. Look up the Scanner class in the Java API documentation. Write four methods that belong to the Scanner class. Write this if statement using the ternary operator: if(age >21) {eligible = true;} else {eligible = false;}

Solution

1. Fields are declared inside a class.

2. Local variables are declared in functions and blocks

3. Execution of java application starts from the main method of the class.

4. As private fields of a class can\'t be accessed directly outside the class itself, we use set() methods to set the values of these variables from outside the class.

5. As private fields of a class can\'t be accessed outside the class itself, we use get() methods to get the values stores in such fields.

6. Fields of a class are declared using private access, so that the users have to use setters and getters to access them. Also it is helpful, when we have to force certain checks or restrictions while accessing the fields; this way, changes will have to be made only in setters/getters, rather than making a change everywhere the class is accessed.

7. a)   Student myStudent;
b)   myStudent=new Student(\"lastName\",\"800-number\");

8. 4 methods belonging to Scanner class:
   nextLine()
   nextInt()
   nextDouble()
   nextBoolean()

9. eligible=age>21?true:false;


Note: For question 7: A string literal is passed in double quotes, while an integer is passed without quotes. But as \'800-number\' contains a hyphen sign as well, we cannot pass it as an integer,hence we are passing it as a String(in double quotes).

 Where are fields declared? Where are local variables declared? Where does the execution of a Java application start? What are set() methods used for? What are

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site