final

Can excessive use of final hurt more than do good?

我只是一个虾纸丫 提交于 2019-12-21 09:18:25
问题 Why are people so emphatic about making every variable within a class "final"? I don't believe that there is any true benefit to adding final to private local variables, or really to use final for anything other than constants and passing variables into anonymous inner classes. I'm not looking to start any sort of flame war, I just honestly want to know why this is so important to some people. Am I missing something? 回答1: Intent. Other people modifying your code won't change values they aren

Default to making classes either `final` or give them a virtual destructor?

心不动则不痛 提交于 2019-12-21 04:48:15
问题 Classes with non-virtual destructors are a source for bugs if they are used as a base class (if a pointer or reference to the base class is used to refer to an instance of a child class). With the C++11 addition of a final class, I am wondering if it makes sense to set down the following rule: Every class must fulfil one of these two properties: be marked final (if it is not (yet) intended to be inherited from) have a virtual destructor (if it is (or is intended to) be inherited from)

Mock final class in Spock

荒凉一梦 提交于 2019-12-21 01:06:21
问题 Can spock mock final classes? If so, how? Search results brought up this gist, which would seem to imply so, but I can't find any examples of doing so. I've also found forum posts that say mocking final classes isn't supported. 回答1: This specification: @Grab('org.spockframework:spock-core:1.0-groovy-2.4') @Grab('cglib:cglib-nodep:3.1') import spock.lang.* class Test extends Specification { def 'lol'() { given: def s = Mock(String) { size() >> 10 } expect: s.size() == 10 } } ends with the

What's the PHP equivalent of a static variable in other languages?

会有一股神秘感。 提交于 2019-12-20 23:26:10
问题 I'm wondering if PHP has a type of variable in classes that functions like static in other languages. And by that I mean all objects of the same class use the same variable and when it's updated on one it's updated on every one. Static is close because it is shared throughout all objects but I need to be able to update it. Will I have to use globals for this? 回答1: I think static is what you want. You can update a static variable, you just have to do it in a "static context" (ie. using the ::

关键字final

女生的网名这么多〃 提交于 2019-12-20 18:59:33
  final表示最后的、最终的、终端的。在java 语言中主要用在以下4中情况。 1.final关键字修饰成员变量   在java中,如果想将一个变量定义为常量,该变量就用关键字final修饰。用final修饰的变量系统就不会为其添加默认值,必须显式赋值或在构造方法里初始化变量,一旦给final变量赋初值后,其值就不能改变。   示例: public class Test{ final int i; public Test(){} public void printMess(){ System.out.println("i="+i); } }    2.final修饰局部变量   在java中,当final修饰局部变量时,可以读取使用该变量的值,但不可以改变该变量的值。   示例: public class Test{ public int pulsOne(final int x){ return ++x; //将x的值加1并返回 } }    3.final关键字修饰方法   在java中,如果某个方法只想被子类继承,不想被子类重写,就将该方法用final修饰。用final修饰的方法称为最终方法。   示例: public class Test{ public final void printInfo(){ System.out.println("该方法不能被重写"); } }

Difference between final keyword, finally block and finalized method in java throught one good example [duplicate]

你离开我真会死。 提交于 2019-12-20 15:37:14
问题 This question already has answers here : In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil? (6 answers) Closed 4 years ago . Often these keywords confuse me. Can any one tell me exactly what the difference between them is? 回答1: final keyword class On a class it means you forbid to have a child class extending yours. public final class finalClass Attribute/Field final MyObject value = new MyObject() means you won't be able to modify the instance of the object.

java访问修饰符

依然范特西╮ 提交于 2019-12-20 12:49:57
java中的修饰符分为类修饰符,字段修饰符,方法修饰符。根据功能的不同,主要分为以下几种。 1、权限访问修饰符 public,protected,default,private,这四种级别的修饰符都可以用来修饰类、方法和字段。 包外 子类 包内 类内 public yes yes yes yes protected no yes yes yes default no no yes yes private no no no yes 2、final修饰符 final的意思是不可变,他可以修饰类、字段、方法。修饰类后类不能被扩展(extends),也就是不能被继承。修饰字段后字段的值不能被改变,因此如果有final修饰字段,应该对字段进行手动初始化。修饰方法后该方法不能被改变,也就是重写。 3、abstract修饰符 abstract是抽象的意思,用来修饰类和方法,修饰类后,该类为抽象类,不能被实例化,必需进行扩展。修饰方法后,该方法为抽象方法必须被子类重写(override)。 4、static修饰符 static用来修饰内部类,方法,字段。修饰内部类说明该内部类属于外部类而不属于外部类的某个实例。修饰字段说明该字段属于类而不属于类实例。修饰方法说明该方法属于类而不属于类实例。 类修饰符 Public 可以从其他类中访问 obstract 本类不能被实例化 final 不能再声明子类

What is the difference between having a class as final and having a class constructor as private

牧云@^-^@ 提交于 2019-12-20 11:44:35
问题 What exactly is the difference between a final class and having a class constructor as private. I know both can't be subclassed(correct me if i am wrong). Is their any difference? 回答1: A final class cannot be extended. It prevents this final class FinalClass { } // and later class ExtendedClass extends FinalClass { // ERROR } This is useful for things like String - you wouldn't want someone to be able to overwrite the logic of String, one of the most commonly used Objects, and be able to, oh

Declaring object as final in java

℡╲_俬逩灬. 提交于 2019-12-20 10:32:25
问题 Can someone clarify the significance of the below code. class A { int i = 10; public void setI(int b) { i = b; } public int getI() { return i; } } class Test { public static void main(String args[]) throws Throwable { final A ob = new A(); ob.setI(10); System.out.println(ob.getI()); } } The object A is declared as final, but I can change value of this object's instance variable and also retrive the updated value. So what are the significance of declaring an object as final. I am aware about

Are final fields really useful regarding thread-safety?

怎甘沉沦 提交于 2019-12-20 10:26:48
问题 I have been working on a daily basis with the Java Memory Model for some years now. I think I have a good understanding about the concept of data races and the different ways to avoid them (e.g, synchronized blocks, volatile variables, etc). However, there's still something that I don't think I fully understand about the memory model, which is the way that final fields of classes are supposed to be thread safe without any further synchronization. So according to the specification, if an