What exactly is a Context in Java? [duplicate]
Asked Answered
C

4

120

I Googled this and read the Java documentation, but I'm a bit confused. Can somebody please explain what a Context is in plain English?

Cameroun answered 12/10, 2010 at 19:1 Comment(9)
In what context? Seriously -- (not a joke)Gritty
@Colin is one second funnier than me.Gritty
Now I'm even more confused. Seriously though, I've been passing around 'this' in my Android apps for Context, without understanding what exactly a Context is. I'd like to know what Context is and why it's required. I guess you could also say my interest has to do with object factories as well.Cameroun
You should probably provide some code example where you as you said, "pass this around as context". It sounds like it is a parameter to a method, perhaps the documentation of the method has a hint of what the context is supposed to be?Josephinajosephine
Do you mean the Android Context? That is very different from the "Context in Java".Skijoring
This question is similar to "What does 'this' mean?"Etherealize
A context is a set of name-to-object bindings.Diapositive
Context is a pattern Context to keep references of other object that can service to this references as Spring IOC container that Services Dependency injection . . .Terreverte
context ≈ global variable. Please check https://mcmap.net/q/40556/-what-is-39-context-39-on-androidMaggee
B
113

In programming terms, it's the larger surrounding part which can have any influence on the behaviour of the current unit of work. E.g. the running environment used, the environment variables, instance variables, local variables, state of other classes, state of the current environment, etcetera.

In some API's you see this name back in an interface/class, e.g. Servlet's ServletContext, JSF's FacesContext, Spring's ApplicationContext, Android's Context, JNDI's InitialContext, etc. They all often follow the Facade Pattern which abstracts the environmental details the enduser doesn't need to know about away in a single interface/class.

Bloodhound answered 12/10, 2010 at 19:6 Comment(4)
How is this useful? Can you give me an example in Android?Cameroun
The "Facade Pattern" link points to a Wikipedia article which contains an useful example in flavor of a "Computer". Does it help? It at least boils down that it manages and controls inner parts of the entire device (CPU/HDD/RAM/GPU/etc) without that the enduser has to worry about. In case of Android, as per the linked Javadoc "It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc."Bloodhound
The major benefit is that you don't need to do it all the "low-level" way. The context will take care about this.Bloodhound
The meaning of CONTEXT changes with the context. Basically, it is used in content of current state of the application/object. It represents environment data and provide access to things like database, UI etcBernard
L
35

A Context represents your environment. It represents the state surrounding where you are in your system.

For example, in web programming in Java, you have a Request, and a Response. These are passed to the service method of a Servlet.

A property of the Servlet is the ServletConfig, and within that is a ServletContext.

The ServletContext is used to tell the servlet about the Container that the Servlet is within.

So, the ServletContext represents the servlets environment within its container.

Similarly, in Java EE, you have EBJContexts that elements (like session beans) can access to work with their containers.

Those are two examples of contexts used in Java today.

Edit --

You mention Android.

Look here: http://developer.android.com/reference/android/content/Context.html

You can see how this Context gives you all sorts of information about where the Android app is deployed and what's available to it.

Lorislorita answered 12/10, 2010 at 19:10 Comment(0)
S
7

Simply saying, Java context means Java native methods all together.

In next Java code, two lines of code need context: // (1) and // (2)

import java.io.*;

public class Runner{
    public static void main(String[] args) throws IOException { // (1)           
        File file = new File("D:/text.txt");
        String text = "";
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null){ // (2)
            text += line;
        }
        System.out.println(text);
    }
}

(1) needs context because it is invoked by Java native method private native void java.lang.Thread.start0();

(2) reader.readLine() needs context because invokes Java native method public static native void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

PS.

That is what BalusC has said about pattern Facade more strictly.

Statutable answered 18/5, 2013 at 14:0 Comment(0)
C
0

since you capitalized the word, I assume you are referring to the interface javax.naming.Context. A few classes implement this interface, and at its simplest description, it (generically) is a set of name/object pairs.

Coracoid answered 12/10, 2010 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.