foundation Flashcards
Enums solo pueden tener public or defult amenos que sean elementos de una clase pueden sser lo que sea
sad
there is no restriction on what a nested class may or may not extend.
a nested class is any class whose declaration occurs within the body of another class or interface.
A top level class is a class that is not a nested class. An inner class is a nested class that is not explicitly or implicitly declared static.
An anonymous class can be declared in a static method.
Classes declared as members of top-level classes can be declared static.
Anonymous classes cannot be declared static.
The modifier static pertains only to member classes, not to top level or local or anonymous classes.
A static nested class can contain non-static member variables. A static nested interface can contain static member variables. A static nested interface is a static interface and so does not have an associated outer instance.
A nested class is any class whose declaration occurs within the body of another class or interface.
Inner class means a NON STATIC class defined inside a class
A top level class is a class that is not a nested class. An inner class is a nested class that is not explicitly or implicitly declared static. A class defined inside an interface is implicitly static.
For each instance of the outer class, there can exist many instances of a non-static inner class
public class A // outer class { static public class B //Static Nested class . It can be used in other places: A.B b = new A.B(); There is no outer instance. { } class C //Inner class. It can only be used like this: A.C c = new A().new C(); Outer instance is needed. { } }
Now consider the following contents of a file named I1.java …
public interface I1 { public void mA(); public interface InnerI1 { int k = 10; public void innerA(); } } Here, interface InnerI1 is implicitly STATIC and so is called as static nested interface. 'k' is a static (& final) member of this interface. This interface can be referred to in other places like:
class MyClass implements I1.InnerI1 { ... }
This is similar to referring a Top Level class.
A non static inner class may have static members if you make them final.
Anonymous inner classes cannot have any ‘extends’ or ‘implements’ clause
If anonymous class is created for interface, it extends Object class and implement that interface, if it is created for a class then it extends that class.
Anonymous inner class can have initialization parameter. (If the class they extend has a corresponding constructor).
Callable
Callable interface is very similar to Runnable interface and is used with the classes of the Executor framework. It has one method with the following signature: V call() throws Exception
there is a fundamental difference between Runnable and Callable. The Thread class, which is resposible for the creation of a new thread and its execution, works only with Runnable (and not Callable).
ScheduledExecutorService:
An ExecutorService that can schedule commands to run after a given delay, or to execute periodically.
The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.
Commands submitted using the Executor.execute(java.lang.Runnable) and ExecutorService.submit methods are scheduled with a requested delay of zero.
Zero and negative delays (but not periods) are also allowed in schedule methods, and are treated as requests for immediate execution.
Non-static nested classes (except method local classes), can have any type of accessibility
Non-static nested classes can contain static members if they are final
A static nested class can contain a non - static inner class.
Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.
a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Declaring a nested class static only means that instances of the class are created without having an outer instance. It does not put any limits on whether the members of the class can be static or not.
hashCode() returns int
The rule is that hashCode must return same value for equal objects.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
Objects of static nested classes can be created without creating instances of their Outer classes.
Anonymous classes cannot define constructors explicitly in Java code.
Anonymous classes cannot be static.
Non-static inner classes can contain final static fields (but not methods).
A class defined inside an interface is implicitly static.
variables of the encapsulating class can an inner class access, if the inner class is defined in an instance method of the encapsulating class?
All static variables
All final instance variables
All instance variables
All final and effectively final automatic variables
automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable’s scope.
Which variables declared in the encapsulating class or in the method, can an inner class access if the inner class is defined in a static method of encapsulating class?
All static variables
All final or effectively final static or automatic variables
Which statements concerning the relation between a non-static inner class and its outer class instances are true?
Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers.
More than one inner instance can be associated with the same outer instance.
An inner class can extend its outer class.
Type erasure means that a compiled java class does not contain any of the generic information that is present in the java file. In other words, the compiler removes the generic information from a java class when it compile it into byte code. For example, List list; and List list; are compiled to the same byte code. Therefore, at run time, it does not matter whether you’ve used generic classes or not and this allows both kinds of classes to interoperate because they are essentially the same class to the JVM. Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.
sad
A Callable cannot be passed to Thread for Thread creation but a Runnable can be. i.e. new Thread( aRunnable ); is valid. But new Thread( aCallable ); is not.
Callable.call() allows you to declare checked exceptions while Runnable.run() does not. So if your task throws a checked exception, it would be more appropriate to use a Callable.