Today I Learned

Some of the things I've learned every day since Oct 10, 2016

95: Static Variables and Methods (Java)

In the Java programming language, both variables and methods within a class can be static or non-static. For a variable or method to be static means that it can be accessed without referencing an instance of the containing class.

If something is non-static, for example a method \texttt{m} belonging to a class \texttt{C}, one must use an instance of \texttt{C} to call \texttt{m}:

\texttt{C c = new C();}

\texttt{c.m();}

whereas if \texttt{m} was static, it could simply be called with reference to the class itself:

\texttt{C.m();}

Static methods can be called with reference to class instances, but this is generally considered poor form.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: