final

学习笔记——final关键字

你说的曾经没有我的故事 提交于 2019-12-02 12:48:23
final描述一种终结器概念,可以实现如下功能: ·定义不能被继承的类 final class Java{} ·定义不能被覆写的方法、常量 final public void connect() ·定义常量:重要应用是定义不可改变的常量 1.假设在程序开发中用0表示关闭,用1表示打开。private int a=1;private int b=0;此时常量可能会发生改变,通过添加fianl关键字限制修改。 2.常量都是定义公共的,为了体现共享性,使用public static final定义全局常量,形式如下:(注意常量名必须大写) public static final int A=1;(公共全局常量) public class Jicheng{ public static void main(String args[]){ final String a="4567";//此时a不能修改,系统默认为常量 String b="123456789"; String c="123"+a+"89";//从池中直接加载数据 System.out.println(c==b);//栈中的c与b指向堆中同一个地址数据内容 } } 输出结果:true public class Jicheng{ public static void main(String args[]){ String a=

composite inheritance: how to assign a final field at sub-class constructor which depends on 'this' value (backward reference)?

≡放荡痞女 提交于 2019-12-02 07:06:20
I use composite classes to group functionalities. But, the class A (with composite A1), got inherited by B (with composite B1), and a behavior existent at A1 is going to be adapted at B1, but the final a1 must be a B1 instance for this to work. Obs.: I have ways to make sure the composite instantiation happens properly (only by its composite partner). Unable to assign a B1 object to a1 final field: class finalFieldTestFails{ class A1{ A1(A a){} } class A{ protected final A1 a1; A(){ this.a1 = new A1(this); } A(A1 a1){ this.a1 = a1; } } class B1 extends A1{ B1(B b){ super(b); } } class B

Why static final variables are accepted in local classes?

余生颓废 提交于 2019-12-02 05:01:37
问题 I've googled this extensively to no avail. I cannot seem to wrap my head around this concept. Why are static final fields accepted in local classes? Such as the following example below: public void sayGoodbyeInEnglish() { class EnglishGoodbye { public static final String farewell = "Bye bye"; public void sayGoodbye() { System.out.println(farewell); } } System.out.println(EnglishGoodbye.farewell); EnglishGoodbye myEnglishGoodbye = new EnglishGoodbye(); myEnglishGoodbye.sayGoodbye(); } In class

java-final关键字

前提是你 提交于 2019-12-02 03:22:13
当final修饰一个类的时候: 不能有任何的子类,因为没有子类,所有final类的方法都不可以重写。 public final class FinalTest { } 当final修饰一个方法的时候: 这个方法为最终方法,不能被覆盖重写 当final修饰一个局部变量的时候: 一次赋值,终生不变。 当final修饰一个成员变量的时候: 必须保证重载的构造的方法都必须对final的变量进行赋值操作,并且无法进行设置 public class FinalMember { private final String name; public FinalMember() { name = "李生"; } public FinalMember(String name) { // TODO Auto-generated constructor stub this.name = name; } public String getName() { return name; } } 来源: https://www.cnblogs.com/lishi-jie/p/11728563.html

Why static final variables are accepted in local classes?

房东的猫 提交于 2019-12-02 01:55:00
I've googled this extensively to no avail. I cannot seem to wrap my head around this concept. Why are static final fields accepted in local classes? Such as the following example below: public void sayGoodbyeInEnglish() { class EnglishGoodbye { public static final String farewell = "Bye bye"; public void sayGoodbye() { System.out.println(farewell); } } System.out.println(EnglishGoodbye.farewell); EnglishGoodbye myEnglishGoodbye = new EnglishGoodbye(); myEnglishGoodbye.sayGoodbye(); } In class EnglishGoodbye the variable farewell is allowed? Why? I'm confused. Why is that allowed but no static

Is final String s = “Hello World” same as String s = “Hello World”?

ぃ、小莉子 提交于 2019-12-01 21:36:56
问题 If a class is defined as final and we declare an instance of the final class... Would it make any difference? or is final in such cases would be redundant? final String s = "Hello World" same as String s = "Hello World" 回答1: When you use final on the variable, it means that it cannot be re-assigned. Consider the following example: public class FinalExample { private final String a = "Hello World!"; // cannot be reassigned private String b = "Goodbye World!"; // can be reassigned public

Is final String s = “Hello World” same as String s = “Hello World”?

筅森魡賤 提交于 2019-12-01 19:46:43
If a class is defined as final and we declare an instance of the final class... Would it make any difference? or is final in such cases would be redundant? final String s = "Hello World" same as String s = "Hello World" When you use final on the variable, it means that it cannot be re-assigned. Consider the following example: public class FinalExample { private final String a = "Hello World!"; // cannot be reassigned private String b = "Goodbye World!"; // can be reassigned public FinalExample() { a = b; // ILLEGAL: this field cannot be re-assigned b = a; } public static void main(String[]

Local variable needs to be declared final

℡╲_俬逩灬. 提交于 2019-12-01 19:42:40
I'm receiving the error "local variable box is accessed from within inner class; needs to be declared final". That seems alright, but I don't really think it's the best solution, so I was hoping maybe someone else can help me out. Here is my code: public void showPublisherBox(JComboBox box) { if (publisherBox == null) { publisherBox = new AddPublisherForm(); publisherBox.setLocationRelativeTo(this); } publisherBox.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { this.populatePublishers(box); } private void populatePublishers(JComboBox box){ box.setModel(db

Scope of final local variable in java

戏子无情 提交于 2019-12-01 17:20:56
Method-Local inner class cannot access local variables because the instance of the method-local inner class may still alive after the method is over. But local variables will vanish once the local method is over. I learned that method-local inner class can access final local variable, does this mean final local variable still alive after the method is over? Alexis King Sort of. Java anonymous inner classes act like "closures," that is, they "close" around the current local state. However, Java only lets these classes close around final variables. If it didn't, the local state of the variable

Why are all anonymous classes implicitly final?

家住魔仙堡 提交于 2019-12-01 16:48:18
问题 According to the JLS: 15.9.5 Anonymous Class Declarations An anonymous class declaration is automatically derived from a class instance creation expression by the compiler. An anonymous class is never abstract (§8.1.1.1). An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous class is always implicitly final (§8.1.1.2) . This seems like it was a specific design decision, so chances are it has some history. If I choose to have a class like this: