Java interview question: final modified local variable problem __java interview questions

Source: Internet
Author: User
/*
Interview questions: Final modification of local variables
Base type: The value of the base type cannot be changed.
Reference type: The address value of a reference type cannot be changed, but the value of the object's heap memory can be changed.
*/
Class Student {
int age = 10;
}


Class Finaltest {
public static void Main (string[] args) {
Local variables are basic data types
int x = 10;
x = 100;
SYSTEM.OUT.PRINTLN (x);
Final int y = 10;
Unable to allocate value for final variable y
y = 100;
System.out.println (y);
System.out.println ("--------------");

Local variables are reference data types
Student s = new Student ();
System.out.println (S.age);
S.age = 100;
System.out.println (S.age);
System.out.println ("--------------");

Final Student ss = new Student ();
System.out.println (Ss.age);
Ss.age = 100;
System.out.println (Ss.age);

Reassigning memory Space
Unable to assign value to the final variable SS
SS = new Student ();
}

}

————————————

The initialization time for the

/*
Final modifier variable
A: A variable that is final decorated can only be assigned one time.
B: Before the construction method completes. (non-static constants)
*/
Class Demo {
//int num = n
Final int num2 = 20;

int num;
final int num2;

{
//num2 = ten
}

Public Demo () {
num =;
Unable to allocate value for final variable num2
num2 =;
}
}


class FinalTest2 {
public static void Main (string[] args) {
Demo d = new demo ();
System.out.println (D.num);
System.out.println (D.NUM2);
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.