Where are fields declared Where are local variables declared
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).
