Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a Context in Java? [duplicate]

Tags:

java

android

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?

like image 993
Christopher Perry Avatar asked Oct 12 '10 19:10

Christopher Perry


People also ask

Why do we use context in Java?

Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).

What is a context in programming?

A programming context can be defined as all the relevant information that a developer needs to complete a task. Context comprises information from different sources and programmers interpret the same information differently based on their programming goal.

What is context in Java EE?

A context usually refers to the interface used to interact with your runtime environment. This one provides to your program a set of features (like security, request handling, etc...) usually needed by all application running in this kind of domain.

How do you pass context?

It is used to return the Context which is linked to the Application which holds all activities running inside it. When we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity Context or “getApplicationContext” to pass the application Context.


1 Answers

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.

like image 78
BalusC Avatar answered Sep 27 '22 16:09

BalusC