On line 3, you're inspecting a value of a context key, Context.Key<T>. On its own, the key is not bound to any specific context. Many different Context can use the same Context key, and each can set its own value for this key. Line 5 is executed within the specific context you created (created on line 1, executed on line 4). Let's call it context A. This specific context has the key Constant.FOO_CONTEXT_KEY is set to the specific value: "Test Value". So line 5 returns the value of key Constant.FOO_CONTEXT_KEY as set in context A.
On Wednesday, January 19, 2022 at 3:17:49 AM UTC-8 [email protected] wrote: > Hi, > > I have following code: > > 1. Context context = Context.current().withValue(Constant.FOO_CONTEXT_KEY, > "Test Value"); > 2. String valueStr = Constant.FOO_CONTEXT_KEY.get(); > 3. System.out.println(valueStr); //Returns null > 4. context.run(() -> { > 5. System.out.println("Context Data is:" + Constant. > FOO_CONTEXT_KEY.get()); //Gives "Test Value" > 6.} > > I am not able to understand why is null returned at line no 3, where as > proper value is returned at line no 5. > -- You received this message because you are subscribed to the Google Groups "grpc.io" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/40613c4d-a40a-43a3-bf58-5a9d7dd37bd6n%40googlegroups.com.
