j8 MethodRef #cheatsheet

Need to developer more low-level insights as QQ…

  • Out of the four kinds of method refs, only AA) static-method and BB) specific-instance kinds are popular. The other two types are obscure.
  • q[ :: ] is used in all four kinds
  • I think the static-method kind is most readable and most intuitive. The javadoc tutorial features a BB example that should be implemented as static method IMHO.
  • a lambda expression has parameter types. A method ref has none and must be converted to a lambda expression. Where does compiler infer the parameter types? I think it is inferred from the calling context.

java enum differences from regular classes

How is an enum class E different from regular classes?

* static field? ok
* inst field? ok
* static method? ok
* inst meethod? ok
* static methods returning an instance of E? ok
* inst methods returning an instance of E? ok

#2 difference — flat tree. enums can’t form a class hierarchy. All enum classes are on the same level — all leaf-nodes under the same parent.

* can an enum E extend ..? E’s superclass is always java.lang.Enum
* subclasse E? no
* E implementing interfaces? ok

#1 difference — constructor:

* constructors? private only => not subclass-able
* how many instances of the class can be created in a JVM? If there are 5 enum values declared, those are the only instances. All the pointers of type E point to one of the 5 instances.
* constructor with arg? ok
* no-arg constructor? ok

java DB entity object HAS-A DAO@@

Say you have an Account.java representing one row in Account table. You have an AccountDAO.java that read/write the table.

Q: Shall Account.java HAS-A AccountDAO instance?
A: I feel Account.java should not import AcountDAO at all. Acocount.java should be compilable without AccountDAO.java. Assumption is, Account.java is a more generic class. It should be usable in other projects (web, xml serialization, file-based …) without database.

Bottom line — If you want AccountDAO to be compile-time dependent on Account.java, then don’t have the reverse-dependency. DAO is one layer above the DB entity objects.

If Account table relates to Customer and Branch tables, it’s perfectly legitimate and perhaps best practice to use HAS-A between these classes. I think Hibernate and many ORM systems do that. These classes exist on the same “layer” so compile-time dependency is perhaps tolerable. To reduce compile-time dependency, use interfaces rather than classes.

entry point in a java reusable component

What does it mean when a java developer says

“download xyz jar”
“Give me your java api”
“You own that api”
“use this java toolkit”

They are referring to some java classes (or interfaces), usually organized into a package. This toolkit is maintained by one team (owner) to be used by other teams.

Now, good OO design requires encapsulation, so there are usually a limited number of entry points to the toolkit. I mean the first thing you write to reference the toolkit. I think there are only 2 entry points — static methods and constructors.

In some toolkits, api methods are instance methods. Question is how do you get the first instance from this toolkit? The entry points are all about instantiation.

Factory are often static methods.

share variables between methods within a class

* a special map instance field to hold all such data. Map of Objects, so casting needed everywhere.

* (laziest) instance field? reserved for instance attributes. Don’t abuse.
* (laziest) static field? reserved for class attributes. Don’t contaminate.
* return a collection? A bit clumsy but recommended by many.
* best option: avoid sharing. Stick to local vars if possible.
* 2nd best: sometimes the shared variable really belongs to another class. That class/object perhaps is already shared between your methods. Essentially use that object as a courier. More work than the laziest.

java abstract classes +! concrete method@@

Often u make it an interface but there are a few justifications

Q: I think an interface must be public ??
A: non-public interface can be nested inside a class.

* all fields in interfaces must be public static final
* all methods in interface must be public
* #1 justification: Say you plan to add concrete methods to your abstract class. You make it abstract now (instead of interface) to forbid subtypes extending something else. Consider the nightmare — U make it an interface, a subtype extends something else, and now u want it to extend your abstract class instead …. the required change could be huge and painful.

multiple inheritance:unrelated classes sharing methods

This is a limited definition of multiple-inheritance — 2 unrelated classes with unrelated base classes need to share a method (evolving). If you can achieve this, then a class C can inherit methods from many classes.

Let’s say the class hosting the shared method is H. If C can somehow inherit the method from H, then C can also inherit other methods from class G, provided G can immitate[1] H.

[1] sometimes impossible.

Solution 2: IS-A -> HAS-A. Strategy Pattern. Put the concrete method into a helper class, and instantiate an instance of it in each of your “unrelated classes”. P 18 [[ head first design patterns ]] FlyBehaviour.java and QuackBehaviour.java

Solution 1: “global method” ie static method in a utility class. Similar to Arrays .java and Collections .java?

My own design: In nextgen-fttp, 2 classes need the same method parseClli() but the 2 classes already have their own parents


public static String[] parseClli(String clli){
String clliBase, clliExt;
if (clli.length() < 9){
return new String[] {clli, ""};
}else{
clliBase = clli.substring(0,8);
clliExt = clli.substring(8, clli.length() );
return new String[] {clliBase, clliExt};
}
}

an obj ref = a remote control

each rcontrol is programmed to a target.
u can program your rcontrol to an Animal, or a Mammal, or a Dog, or erase all programming.

When your rcontrol is programmed for an Animal, bark() method is not available — the “bark” button is in a detachable extension and removed from the rcontrol. You need to cast.

Q: objects are passed by…?
A: well, u duplicate the rcontrol. A method receiving an arg need to duplicate the rcontrol and can’t reprogram the original rcontrol

Q: FINAL var (of a ref type) means?
A: non-reprogramable

public^private methods invoked by..

Background: We see many method names that confuse some readers as to what objects would invoke them.

Q: which object will call “my” public methods?
A: Client objects. Public methods in myClass.java constitute the expected services presented to “consumers/customers”. Quintessential examples are accessors and constructors. Another instance of myClass often call them too, in some designs.

Q: who will call “my” private methods?
A: only the /hosting/ class or a nested class. This case is simpler than the “public” case.

equals() needed in most beans, entity/domain objects

Q: Suppose you are writing your own class C.java, and in other classes you plan to use List or Set. What method(s) do you need to write in C.java? What if you don’t write the method(s)?I know toString() is not something C.java must have.

A: equals(). Object.equals() uses reference comparison. Without overriding equals(), if you call Collection.contains() or Collection.remove(), you can’t pass in an identical carbon copy. A caron copy won’t pass contains() or remove().

Now most “noun” objects need to be usable in a collection and need meaninful contain() or remove() operations.

Note, once you override equals(), you need to override hashCode(). http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object). A Set and a Map are even more dependent on equals().

what makes a class abstract

If compiler detects an abstract method, the (“host”) class must be declared abstract.

I think this is the only difference between abstract class and non-abstract class.

U can convert a non-abstract class into abstract by adding the keyword “abstract” to the class. All attributes are left alone. Yes, an abstract class can CONTAIN instance-level fields as well as static (ie class-level) members.