Introduction
Java allows you to define a class within another class.
class OuterClass { ... class NestedClass { ... } }
There a two types of member classes inner classes
and static nested classes
class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... } }
-
A nested class is a member of its enclosing class.
-
A member class can be declared private, public, protected, or package private.
|
Warning
|
If a member class is declared public or protected, the class is being exported as an API. Any change from inner to static nested class would break binary compatibility!
|
There are local classes
public class OuterClass { ... public boolean block(String parameter){ ... class LocalClass{ } ... } ... }
-
Local classes are always defined inside a block.
-
Local classes can access the members of its enclosing class.
-
Local classes are non-static because they have access to instance members of the enclosing block.
There are anonymous classes
public class OuterClass { interface NewType{ public void foo(); } public boolean block(String parameter){ ... NewType object = new NewType(){ @Override public void foo(){ ... } } ... } ... }
-
Anonymous classes are like local classes except that they do not have a name.
-
Anonymous classes are expressions defined in another expression.
-
Anonymous classes make your code more concise.
Inner class
-
An inner class has access to other members of the enclosing class, even private members.
-
An inner class cannot define any static members itself, except constants.
-
An inner class is associated with/tied to an instance of its enclosing class, e.g.
InnerClass
can exist only within an instance ofOuterClass
. -
This association or superfluous reference costs extra space and time, because if one of the objects is used both objects are not eligible for garbage collection.
-
Use this class ONLY if you have to access nonstatic members of the enclosing class!
//First OuterClass has to be created! OuterClass outerObject = new OuterClass(); //Then InnerClass can be created. OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Static nested class
-
A static nested class cannot refer directly to instance variables or methods defined in its enclosing class.
-
A static nested class can only access static members of it’s enclosing class.
-
A static nested class can be instantiated!
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Local classes
-
Local classes are defined in a block
{...}
like a method body or anif
clause and so on. -
To keep up a good readability the implementation should be short!
public class OuterClass { private String regularExpression = "[^0-9]"; public boolean validatePhoneNumber(String phoneNumber){ int numberLength = 10; class PhoneNumber{ ... /** * Access member of enclosing class - regularExpression */ private String filterNumberForDigits(String rawNumber){ return rawNumber.replaceAll( regularExpression , ""); } /** * Access variable of enclosing block - numberLength - has been captured! */ private String validateNumberForLength(String filteredNumber){ //numberLength = 7; return ( filteredNumber.length() == numberLength) ? filteredNumber : null; } } ... } }
-
Local classes can access the members (e.g. regularExpression ) of its enclosing class.
Captured variable
-
Local classes can access the local variables (e.g. numberLength ) or parameters of its enclosing block. - This is called capturing.
-
Below Java 8 local classes could only access local variables that are declared final.
-
In Java 8 local classes can access local variables that are declared final or are effectively final.
-
A variable or parameter whose value is never changed after it is initialized is effectively final.
-
In case that a captured variable is trying to be assigned (e.g. numberLength = 7;) the following error occurs: local variables referenced from an inner class must be final or effectively final.
Static declarations in local classes
-
Local classes are in that case similar to inner classes!
-
Local classes are non-static because they have access to instance members of the enclosing block.
-
An constant like
private static final int CONST = 1000
can be declared, most other static declarations not. -
E.g. An
interface
is inherently static and can’t be declared inside a block.
public void greetInEnglish() { interface HelloThere { public void greet(); } class EnglishHelloThere implements HelloThere { public void greet() { System.out.println("Hello " + name); } } HelloThere myGreeting = new EnglishHelloThere(); myGreeting.greet(); }
Anonymous class
-
Anonymous classes enable you to declare and instantiate a class at the same time inside a block
{...}
. -
To keep up a good readability the implementation should be short!
public class OuterClass { private String regularExpression = "[^0-9]"; interface PhoneNumber{ public String getFormattedPhoneNumber(String rawNumber); } public boolean validatePhoneNumber(String phoneNumber){ /** * Create instance with 'new' is like a constructor invocation. */ PhoneNumber processedNumber = new PhoneNumber(){ @Override public String getFormattedPhoneNumber(String rawNumber) { return formatNumber(rawNumber); } ... /** * Access member of enclosing class - regularExpression */ private String filterNumberForDigits(String rawNumber){ return rawNumber.replaceAll( regularExpression , ""); } ... }; ... } }
-
The syntax of an anonymous class expression is like the invocation of a constructor inside a block of code. A new Instance is being created.
-
Like local classes, anonymous classes can access the members of its enclosing class.
-
Like local classes, anonymous classes can access the local variables or parameters of its enclosing block. - This is called capturing.
-
Similar to local classes those variables or parameters has to be final or effectively final.
-
Anonymous classes also have the same static restrictions as local classes with respect to their members.
-
Like nested classes, shadowing is possible in case of declarations of same type with same name in inner and outer class.
Lambda Expressions
-
Like anonymous classes has lambda expressions the intention to pass functionality as an argument.
-
Before Java 8 it was always necessary to create an instance with new belonging to a class that has a method with the desired code.
-
Lambda expression evaluation does NOT cause the execution of the expression’s body, instead this may occur at a later time when an appropriate method of the functional interface is invoked.
Syntax
-
A lambda expression is like a method declaration: it provides a list of formal parameters and a body expressed in terms of those parameters.
-
Parameters
->
Body
-
Cause of the missing methodName consider lambda expressions as anonymous methods.
(int x, int y) -> x + y // Lambda expression add(int x, int y){ // Method declaration return x + y; }
Parameter
-
A comma-separated list of formal parameters.
-
The formal parameters of a lambda expression may have either explicitly declared types or implicitly inferred types. These styles cannot be mixed!
(int x, int y) -> x + y // Declared-type parameters (x, y) -> x + y // Inferred-type parameters
-
Omit the parentheses too if there is only one parameter.
x -> x * x // Inferred-type parameter, no parantheses
-
If the formal parameters have inferred types, then these types are derived from the functional interface type targeted by the lambda expression. See target type.
Token
-
->
separates the parameter list from the body.
Body
-
Like a method body, a lambda body describes code that will be executed whenever an invocation occurs.
-
A lambda body is either a single expression or a block.
-
If you specify a single expression, then the Java runtime evaluates the expression and then returns its value.
x -> x * x
-
In case of a block, the body has to be ether void-compatible OR value-compatible.
-
A block lambda body is void-compatible if every return statement in the block has the form
return;
() -> {} () -> { System.out.println("done"); } // Returns 'void' () -> System.out.println("done") // A single void method invocation does NOT need braces!
-
A block lambda body is value-compatible if it cannot complete normally (e.g. is not reachable, caused by
if
clause) and every return statement in the block has the formreturn Expression;
.
() -> { return "done"; } // Returns a 'String' value () -> { if (...) return 1; else return 0; } // In every case a value is returned!
-
It is a compile-time error if a block lambda body is NEITHER void-compatible NOR value-compatible.
() -> { if (...) return "done"; System.out.println("done"); } // Error!
Using a lambda expression instead of an anonymous class
-
We are going to sort an ArrayList populated with Persons.
public class Person { private String name; private int age; ... }
-
The order should be based on the
age
of an person, so we need to implement anComparator<Person>
interface. -
The sorting itself is done by the
Collections.sort(List<T> list, Comparator<? super T> c)
utility method.
// Java 7 - using an anonymous class Collections.sort(persons, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return Integer.compare(o1.getAge(), o2.getAge()); } }); // Java 8 - using a lambda expression Collections.sort(persons, (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));
-
But how does the compiler know that Comparator can be substituted by a lambda expression?
-
Because the Comperator interface is annotated with
@FunctionalInterface
.
Functional Interfaces
-
Is a simple interface that contains only ONE abstract method.
-
Evaluation of a lambda expression produces always an instance of a functional interface.
-
So behind the scenes an object is created to maintain backwards compatibility.
-
In conclusion there is always possible to use a lambda expression instead of an anonymous class.
-
See Oracle Java Spec: Not so obvious cases with redeclared methods from
Object.class
liketoString()
orclone()
.
Capturing
-
Excellent descripton of closures/capturing in RealImpatient8-P.10
-
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared final or be effectively final.
-
Any local variable used but not declared in a lambda body must be definitely assigned before the lambda body.
example missing: compare anonymous class capturing with lambda capturing
Scope, Name conflicts, Shadowing
-
Unlike code appearing in anonymous class declarations, the meaning of names and the
this
andsuper
keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context. -
Except that lambda parameters introduce new names.
example missing: compare anonymous class scope with lambda scope
Target Type
-
To determine the type of a lambda expression, the Java compiler uses the target type of the context or situation in which the lambda expression was found.
-
You can only use lambda expressions in situations in which the Java compiler can determine the target type, like:
-
Variable declarations
-
Assignments
-
Return statements
-
…
-
-
Further example: overload resolution and type argument inference
Checked Exceptions
-
How to handle exceptions thrown inside the lambda body?
-
RealImpatient-P.8
Method References
-
Method references provide easy-to-read lambda expressions for methods that already have a name.
Constructor References
…
Default Methods
…
Static Methods in interfaces
…
When to use what?
Nested classes
-
If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together.
-
To increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A’s members can be declared private and B can access them. In addition, B itself should be hidden from the outside world, with a
private
constructor.
|
Note
|
A nested class should ONLY serve its enclosing class! If a nested class would be useful in some other context, then it should be a top-level class! |
Inner class
-
Inner classes are eligible if you need access to the instance members of its enclosing class.
-
Inner classes are eligible if you don’t require access to local variables or parameters (capturing).
Static nested class
-
Static nested class are eligible if you need access to the static fields and functions of its enclosing class.
-
Inner classes are eligible if you don’t require access to local variables or parameters (capturing).
Local class
-
Local classes are eligible if you need a new named type.
-
Local classes are eligible if you need more than one instance.
-
Local classes are eligible if you need to access its constructor.
-
Local classes are eligible if you need access to the members of its enclosing class.
Anonymous class
-
Anonymous classes are eligible only if one instance is needed.
-
Anonymous classes are ideal for implementing an interface that contains two or more methods.
-
Anonymous classes are eligible if you need access to the members of its enclosing class.
Shadowing
-
If a declaration of a type of the inner scope has the same name as another declaration in the outer scope, then the declaration shadows the declaration of the outer scope.
-
You cannot refer to a shadowed declaration by its name alone.
|
Note
|
Inner scope shadows outer scope |
public class ShadowTest { public int x = 0; class FirstLevel { public int x = 1; void methodInFirstLevel(int x) { //x = 23 System.out.println("x = " + x); //this.x = 1 System.out.println("this.x = " + this.x); //ShadowTest.this.x = 0 System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); } } public static void main(String... args) { ShadowTest st = new ShadowTest(); ShadowTest.FirstLevel fl = st.new FirstLevel(); fl.methodInFirstLevel(23); } }
Serialization
-
Serialization of inner classes, including local and anonymous classes, is strongly discouraged.
-
Possible compatibility issues can occur if you serialize an inner class and then deserialize it with a different JRE implementation.


Plugin
Undocumented
-
Whether or not the plugin is active
Declaration
Swift
var active: Bool
-
Whether or not the plugin is bundled
Declaration
Swift
var bundled: Bool?
-
Whether or not the plugin is deleted
Declaration
Swift
var deleted: Bool?
-
Whether or not the plugin is downgradable
Declaration
Swift
var downgradable: Bool?
-
Whether or not the plugin is enabled
Declaration
Swift
var enabled: Bool?
-
Whether or not the plugin has an update
Declaration
Swift
var hasUpdate: Bool?
-
The plugin’s long name
Declaration
Swift
var longName: String?
-
Whether or not the plugin is pinned
Declaration
Swift
var pinned: Bool?
-
The plugin’s short name
Declaration
Swift
var shortName: String
-
Whether or not the plugin supports dynamic load
Declaration
Swift
var supportsDynamicLoad: String?
-
The plugin’s url
Declaration
Swift
var url: URL?
-
The version of the plugin
Declaration
Swift
var version: String?
-
The plugin’s dependencies
Declaration
Swift
var dependencies: [Dependency] = []
-
Optionally initialize a Plugin
Declaration
Swift
init?(json: [String: AnyObject])
Parameters
json
The json from which to initialize the Plugin
Return Value
The initialized Plugin object or nil, if initialization failed


%inherit file="/layout/wizard.html"/> <%namespace file="/lib.html" import="wizard_save_search_form"/>
${_('Save the search before basing an alert on it.')}


The extreme heat that Chicago is capable of experiencing during the height of the summer season can persist into the autumn season. Temperatures have reached 100 degrees as late as September 7 (with 99 °F or 37 °C occurring as late as September 29), and temperatures have reached the lower-to-mid 90s Fahrenheit (low 30s Celsius) as late as October 6. Conversely, temperatures have dropped below freezing overnight as early as September 23, and subzero temperatures (below −18 °C) have arrived as early as November 23. Although extremely rare, temperatures at or above 70° have been recorded into early December, most recently in 2012 when 70 °F (21 °C) was recorded on December 3, some surrounding areas reaching temperatures as high as 72 °F (22 °C) to 75 °F (24 °C).
Fall, in some ways, is a calmer season than any of the other three in Chicago. This does not mean that wild weather can't happen. The first freeze of the season on average occurs on October 24 at Midway and 11 days earlier at O'Hare. In many years, a period of warm weather, known as an Indian summer, occurs well after the autumnal equinox has occurred. In these so-called Indian summers, extremely warm weather can last for several days. For example, during the 2005 American League Division Series, for both home games between the Chicago White Sox and the Boston Red Sox, temperatures exceeded 80 °F (27 °C), despite the fact that it was already well into October. On top of that, during the same warm stretch in October 2005, for two consecutive days the overnight temperature failed to drop below 70 °F (21 °C), an extraordinary occurrence for Chicago in early autumn. Fall can bring rain showers, many of which are capable of producing flooding. As the winter solstice nears, the threat of a major winter or snowstorm grows, and there have been major winter storms around the Thanksgiving holiday, causing major delays at the city's two major airports. The first measurable (≥0.1 in or 0.25 cm) snow on average falls on November 19. However, on December 20, 2012, the record for latest first measurable snowfall, besting the record of December 17, 1899. The autumn and early winter of 2012-13 did not see a daily maximum temperature below freezing 32 °F (0 °C) until early January 2013, and the entire calendar year of 2012 did not record a temperature lower than 5 °F (−15 °C). The largest snowstorm before the winter solstice dropped 14.8 inches (380 mm) at Midway Airport in December 1929.


Facebook no longer allows us to get your friends list automatically. Please go to your friends list manually and upload a copy of the resulting HTML file here below.
{% if error %}
{{ error }}
{% endif %}
{% endif %}
{% else %}
Log in here
{% endif %}


--- layout: default footerlogos: yes title: "Agenda – Day 1" lang: en ---
28.04.2017
Friday at BMVI
We are excited to receive you at the Federal Ministry of Transport and Digital Infrastructure on the first day.
At the end of the first day we want to use the opportunity that so many smart minds with experience in open data are gathered at in one place and ask you to think of ideas on how to increase Open Data in Germany. These ideas can be further discussed on the second day.
Program for the second day


Creativity Under Construction Internet and Virtual Reality and Community Projects - Go to top About Website created by Nathan Leigh Website tools created by Mary Lou. Picture of my cat Kizzy on the right.


I made a Java desktop app where the aim of game is to simply guess the computer's number. I wanted to make the GUI more modern so I used JavaFX to do just that. I enjoyed using Java's lambda expressions to help me write event handler functions. This project showed me how fun coding can be.