final

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

♀尐吖头ヾ 提交于 2019-12-03 14:49:21
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) Probably there are cases were neither of these two options makes sense, but I guess they could be treated as

what is the use of keyword final?

非 Y 不嫁゛ 提交于 2019-12-03 11:33:26
In the below code if i remove the keyword final from EditText i am an getting error in the line (6) where i pass EditText object (et) to the intent...I have to knw the significance of final keyword here... final EditText et=(EditText)findViewById(R.id.t); Button b=(Button)findViewById(R.id.b1); b.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v)<br> { Intent on=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+et.getText())); startActivity(on); } }); It is because you use closure here. It means that inner class uses the context of the inbounded one. To use it the

What does it mean for a collection to be final in Java? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-03 11:16:11
This question already has an answer here: Why can final object be modified? 7 answers What does it mean for a collection to be declared final in Java? Is it that no more elements can be added to it? Is it that the elements already there cannot be changed? Is it something else? No. It simply means that the reference cannot be changed. final List list = new LinkedList(); .... list.add(someObject); //okay list.remove(someObject); //okay list = new LinkedList(); //not okay list = refToSomeOtherList; //not okay You are getting confused between final and immutable Objects. final --> You cannot

Why can't we set the value of static final variable in static block through class name [duplicate]

泪湿孤枕 提交于 2019-12-03 08:33:56
问题 This question already has answers here : Why isn't a qualified static final variable allowed in a static initialization block? (2 answers) Closed 4 years ago . For example, consider code snap below: public static final int a; public static final int b; static { a = 8; // it's working Test.b = 10; // compilation error Test.b cannot be assigned. } Why can't we use Test.b = 10; inside a static block of the Test class itself? Without the class name it's working fine. Is there any reason behind

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

和自甴很熟 提交于 2019-12-03 08:17:13
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? 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 :: operator. class Class1 { protected static $_count = 0; public function incrementCount() { return self::$

Static Final Variable in Java [duplicate]

ぃ、小莉子 提交于 2019-12-03 08:11:03
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: private final static attribute vs private final attribute What's the difference between declaring a variable as static final int x = 5; or final int x = 5; If I only want to the variable to be local, and constant (cannot be changed later)? Thanks 回答1: Just having final will have the intended effect. final int x = 5; ... x = 10; // this will cause a compilation error because x is final Declaring static is making

Why String is immutable or final in Java [duplicate]

我是研究僧i 提交于 2019-12-03 07:14:02
This question already has answers here : Why is the String class declared final in Java? (16 answers) As i was told this is important String Interview question in Java, which starts with discussion of " What is String ", how String is different in java than in C or C++ and then you are asked about immutable objects and you're asked the main question: " Why String is immutable or final in Java ". Can you share your Ideas ? Thanks in advance. It is mainly for security reasons . String is used as parameter in network connection, database url etc. It can be easily attacked if it is mutable

Mock final class in Spock

旧街凉风 提交于 2019-12-03 06:58:28
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. Opal 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 following exception: JUnit 4 Runner, Tests: 1, Failures: 1, Time: 29 Test Failure: lol(Test) org

How to prevent the changing of a variable value in Javascript [duplicate]

*爱你&永不变心* 提交于 2019-12-03 06:55:35
Possible Duplicate: Are there constants in Javascript? Is there a way to declare a static of final value in javascript so that it cannot be altered by a 3rd party? What I have is a article sharing application, with free users being ad supported. What I wish to do is prevent the free users from altering the innerHTML content by altering the storage variable for the content. What I have at this moment is a timer that reloads the innerHTML of the article on user website every 5 seconds and I'm storing the value for the reload in a variable. However, if a genius using jsbeatify explores which

Creating an immutable object without final fields?

匆匆过客 提交于 2019-12-03 06:51:35
Can we create an immutable object without having all fields final? If possible a couple of examples would be helpful. Declare all fields private and only define getters: public final class Private{ private int a; private int b; public int getA(){return this.a;} public int getB(){return this.b;} } citing @Jon Skeet's comment, final class modifier is useful for: While an instance of just Private is immutable, an instance of a subclass may well be mutable. So code receiving a reference of type Private can't rely on it being immutable without checking that it's an instance of just Private. So if