标签: final

c ++最终常量字符串

在Java中,我们可以指定一个字符串final来声明'常量'.例如

static final String myConst = "Hello";
Run Code Online (Sandbox Code Playgroud)

这样在c ++中这样做的正确方法是什么?

const char * const myConst = "Hello";
Run Code Online (Sandbox Code Playgroud)

我一直看到人们这样做:

const char * myConst = "Hello";
Run Code Online (Sandbox Code Playgroud)

但实际上,您可以更改指针指向的内容.那么,为什么人们不将指针声明为常量呢?这样做的正确方法是什么?

c++ pointers final const

1
推荐指数
2
解决办法
2146
查看次数

即使通过不同的路径重新分配,使用"final"是否保持原始引用和对象仍然有效?

我想知道是否final真正"冻结"其引用,迫使其底层对象与通过不同路径对原始引用进行的任何重新分配共存,例如

class ThingHolder{
    Thing thing;
}

class Thing{
    int i=5;
    Thing(int i){
        this.i=i;
    }
}

public static void main(String[] args){
    ThingHolder thingHolder = new ThingHolder();
    thingHolder.thing = new Thing(5);

    final Thing aFinalReference = thingHolder.thing;

    thingHolder.thing = new Thing(6); //will this now coexist with "aFinalReference"?

    //... 
}
Run Code Online (Sandbox Code Playgroud)

aFinalReference现在将作为独立对象持续存在,尽管不再是其中的一部分thingHolder,并且继续引用原始的Thing(即int当前为5的那个),不管现在发生了thingHolder什么?

java final

1
推荐指数
1
解决办法
121
查看次数

在JButton内部执行的最终变量是否必需?

所以我有一个JList,我试图在一个内部使用它JButton小号actionPerformed方法,它要求我做出JList final这是为什么,下面的代码片段

public SomeClass() {    
  btnNewButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         list.clearSelection();             
    }});
}
Run Code Online (Sandbox Code Playgroud)

我实际上没有问题使它成为最终的,我只是不确定为什么我需要.

java swing final jbutton jlist

1
推荐指数
1
解决办法
1768
查看次数

为最终变量赋值

假设我有这个代码:

public HttpResponse myFunction(...) {
    final HttpResponse resp;
    OnResponseCallback myCallback = new OnResponseCallback() {
        public void onResponseReceived(HttpResponse response) {
            resp = response;
        }
    };
    // launch operation, result will be returned to myCallback.onResponseReceived()
    // wait on a CountDownLatch until operation is finished
    return resp;
}
Run Code Online (Sandbox Code Playgroud)

显然我无法从onResponseReceived为resp赋值,因为它是一个最终变量,但如果它不是最终变量onResponseReceived则看不到它.那么,如何从onResponseReceived为resp赋值?

我想的是创建一个封装resp对象的包装类.最后一个对象将是这个包装类的一个实例,我可以将值赋给resp处理最终类中的对象(这不是最终的).

代码就是这个:

class ResponseWrapper {
    HttpResponse resp = null;
}

public HttpResponse myFunction(...) {
    final ResponseWrapper respWrap = new ResponseWrapper();
    OnResponseCallback myCallback = new OnResponseCallback() {
        public void onResponseReceived(HttpResponse response) {
            respWrap.resp = response; …
Run Code Online (Sandbox Code Playgroud)

java final callback

1
推荐指数
1
解决办法
1668
查看次数

Java中的最终和不可变变量

我有一个Path类,我认为它是不可变的.在另一个名为Test的类中,我有一个Path对象的最终引用.

然而,在构造函数和getter方法之间,即使Path对象是不可变的并且引用是final,Path对象也会更改.我知道这一点,因为Path中int数组节点的长度从构造函数变为getter.看起来这个对象完全是一个完全不同的对象.

我的程序是多线程的,但我已尝试使用单个线程,但没有解决问题.

这是不可变的Path类

public class Path implements Iterable<Point> {

private final int[] nodes;
private final double distance;

    public Path(Scenario scenario, int gateway, int sensor){
        this.scenario = scenario;
        nodes = new int[2];

        nodes[1] = -gateway - 1;
        nodes[0] = sensor;

        distance = scenario.DISTANCE_GATEWAY_SENSOR[gateway][sensor];
    }

    public Path(Path base, int newSensor){
        scenario = base.scenario;

        //Copy the old path. These are rigid structures so that we do not need to deep copy
        nodes = new int[base.nodes.length + 1];
        for(int i = 0; i …
Run Code Online (Sandbox Code Playgroud)

java variables final immutability

1
推荐指数
1
解决办法
3557
查看次数

理解类的地图字段上的最终静态用法

我是c ++开发人员,并试图熟悉Core Java概念.我发现这个令人困惑的是我的最终静态是在构造对象后无法改变的东西.(如果我错了,请纠正我)

我遇到了下面的实现,我发现这令人困惑,因为它似乎允许将值添加到Map中,即使它是最终静态的

public class MockUserServiceImpl implements UserService {

    private static final Map<String, User> userMap = new HashMap<String, User>();

    public static void addUserToCache(int userId, String userName) {
        if (!userMap.containsKey(userName)) {
            userMap.put(userName, new User(userId, userName));
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

有人可以试着解释一下这里的静态决赛是什么意思

java encapsulation final static-members

1
推荐指数
1
解决办法
349
查看次数

如何从Java中的多个终结字符串中随机选取一个字符串?

正如问题所说,我在最后一堂课中有多个决赛字符串,如下所示:

public final class MyStrings {

    public static final String stringOne = "this is string one";
    public static final String stringTwo = "this is string two";
    public static final String stringThree = "this is string three";
    public static final String stringFour = "this is string four";
    public static final String stringFive = "this is string five";

}
Run Code Online (Sandbox Code Playgroud)

我知道将它们放在列表或数组中会更容易,但我想避免运行时填充这些结构.可能吗?从该类中随机选择字符串的最简单方法是什么?谢谢.

java string random final

1
推荐指数
2
解决办法
2674
查看次数

"static final int"是什么意思?

我是java新手,这本书出人意料地开始使用它而不解释它甚至一次.为什么堆栈溢出要求我写的比实际需要的还多?

java final

1
推荐指数
2
解决办法
4万
查看次数

可以重新初始化接口变量吗?

interface abs{
 int a=10;// by default final static
     void callme(); 
}
class B implements abs{
    int a =11;// reinitializing

    void call()
    {
            System.out.println("No problem"+a);
    }
    public void callme()
    {
            System.out.println("Call me"+a);
    }

}
class main{


    public static void main(String args[]){

        B m=new B();
        m.call();
        m.callme();
    }

}
Run Code Online (Sandbox Code Playgroud)

在Herbert Schildt的书中,我读过接口变量是默认的Final和static.这隐含地意味着它将像一个常量.但是当我在上面提到的代码中为变量a分配11时,它没有给出任何错误.

o/p没问题11打电话给我11

java static final interface

1
推荐指数
1
解决办法
125
查看次数

为什么Java变量不经常被声明为`final`?比较swift中的let关键字

在swift中,let如果变量在初始化后没有改变,建议用关键字声明变量.我看到很多变量被声明为let.

在java中,我认为关键字final可以起到同样的作用.但是根据我有限的经验,我只看到人们final在极少数情况下声明变量,例如PI.

为什么Java变量不final经常被声明?有什么我想念的吗?

java variables final let swift

1
推荐指数
1
解决办法
237
查看次数