LordPankake

Explaining some java code

Feb 7th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 KB | None | 0 0
  1. /*
  2.  * Your professor was trying to show you "order of operations" in a sense, but it is poorly structured.
  3.  * Below is the code that your original code compiles into. I will explain why things are moved.
  4.  */
  5. public class M {
  6.     // ====================================
  7.     //             THE CONSTRUCTOR
  8.     // ====================================
  9.     //  | A constructor is called in any line
  10.     //  | that looks like "new MyType(...)"
  11.     //  | The "..." means there can be any
  12.     //  | number of arguments. In this case
  13.     //  | there are none.
  14.     //  |
  15.     //  | In the original source file you saw:
  16.     //  |
  17.     //  |  . . . . . . . . . . . . . . . . .
  18.     //  | {
  19.     //  |   System.out.println("Scope1");
  20.     //  | }
  21.     //  |
  22.     //  | M() {
  23.     //  |   System.out.println("Constructor");
  24.     //  | }
  25.     //  |
  26.     //  | {
  27.     //  |   System.out.println("Scope2");
  28.     //  | }
  29.     //  |  . . . . . . . . . . . . . . . . .
  30.     //  |
  31.     //  | In the code above (from the original)
  32.     //  | there were these 'random' blocks with
  33.     //  | no name. When you compile these, they
  34.     //  | are placed in the BEGINNING of
  35.     //  | the constructor in the order of they
  36.     //  | appear in the source.
  37.     M() {
  38.         System.out.println("Scope1");
  39.         System.out.println("Scope2");
  40.         System.out.println("Constructor");
  41.     }
  42.  
  43.     // ====================================
  44.     //        THE INSTANCE METHOD
  45.     // ====================================
  46.     //  | This method belongs to "M".
  47.     //  | I think the professor was trying
  48.     //  | to show you that you can declare
  49.     //  | extensions of classes in-line
  50.     //  | through various ways.
  51.     public void test() {
  52.         // Here we make a new "K" but we
  53.         // override "print". This new
  54.         // implementation of "K" is stored
  55.         // in the variable "k".
  56.         K k = new K(){
  57.             @Override
  58.             public void print() {
  59.                 System.out.println("Anonymous K print");
  60.             }
  61.         };
  62.         // We now call "print" for "k".
  63.         k.print();
  64.         // Here we do the same thing as above
  65.         // but instead of storing the new
  66.         // implementation in a variable,
  67.         // we call the "print" method in-line.
  68.         new K(){
  69.             @Override
  70.             public void print() {
  71.                 System.out.println("Anonymous K");
  72.             }
  73.         }.print();
  74.         // Same as above with in-line, but we
  75.         // add a new method "smth". You can
  76.         // call this method in-line too even
  77.         // though it does belong to the original
  78.         // "K" class defined at the bottom of this
  79.         // file.
  80.         // The truth is, all of these in-line classes
  81.         // generate new files when they are compiled.
  82.         // They are called "Anonymous" classes.
  83.         // Since they behave as independant classes
  84.         // you can define new methods in them and
  85.         // even call them like in this example.
  86.         // However if you were to store this in a variable
  87.         // of type "K" with the name "test" you would
  88.         // be unable to do "test.smth()" because
  89.         // "smth" does not belong to K, it belongs
  90.         // to the anonymous class.
  91.         new K(){
  92.             public void smth() {
  93.                 System.out.println("Anonymous K2");
  94.             }
  95.  
  96.             @Override
  97.             public void print() {
  98.                 System.out.println("Anonymous K2 print");
  99.             }
  100.         }.smth();
  101.     }
  102.    
  103.     // ====================================
  104.     //     THE MAIN (Entry-point) METHOD
  105.     // ====================================
  106.     //  | This is where your java program
  107.     //  | typically begins & ends. As you can
  108.     //  |  see there are two "println" calls.
  109.     //  |   One in the beginning.
  110.     //  |   One in the end.
  111.     //  | In between a new instance of "M" is
  112.     //  | created. That will call the constructor.
  113.     //  | Then it calls the instance method of "M"
  114.     //  | called "test".
  115.     //  |
  116.     //  | After test is run the ending print is
  117.     //  | called and the program will terminate.
  118.     //  |
  119.     //  | There is a "gotcha" in this class though.
  120.     //  | You have a 'static` block below.
  121.     //  | Before ANYTHING happens with a class
  122.     //  | java will check if it has a "static block"
  123.     //  | and will run the code inside there first.
  124.     //  | Yes, it runs before constructors.
  125.     //  | Yes, it runs before the main method.
  126.     public static void main(String[] arrstring) {
  127.         System.out.println("Main1"); // First line executed
  128.         M m = new M();
  129.         m.test();
  130.         System.out.println("Main2"); // Last line executed
  131.     }
  132.  
  133.     // ====================================
  134.     //           THE STATIC BLOCK
  135.     // ====================================
  136.     //  | This is a special method sort of
  137.     //  | like a constructor, but it is even
  138.     //  | more unique.
  139.     //  | The static block is only called ONCE.
  140.     //  | This occurs when the class is loaded
  141.     //  | by the java virtual machine.
  142.     //  |
  143.     //  | Since this is also the class with
  144.     //  | the "main" method, it is loaded instantly.
  145.     //  | This method precedes all others.
  146.     //  |
  147.     //  | If we had another class "O" in a file
  148.     //  | "O.java" we could do the following:
  149.     //  |
  150.     //  |       new O();
  151.     //  |
  152.     //  | Java will check if it's loaded, and if
  153.     //  | it is not, it will load the class, then
  154.     //  | check if it needs to run the static block.
  155.     //  | If one exists it runs it. Lastly it does
  156.     //  | what we told it to do, call the constructor.
  157.     static {
  158.         System.out.println("Static");
  159.     }
  160.  
  161.    
  162.     // ====================================
  163.     //           THE INNER CLASS
  164.     // ====================================
  165.     //  | This is just a class within a class.
  166.     //  | In our case it has:
  167.     //  |  * Constructor with no args
  168.     //  |  * abstract "print" method.
  169.     //  |
  170.     //  | The class is abstract so the following
  171.     //  | code is invalid:
  172.     //  |
  173.     //  |    K variable = new K();
  174.     //  |
  175.     //  | However the following code IS valid:
  176.     //  |
  177.     //  |    K variable = new K(){
  178.     //  |       @Override
  179.     //  |       public void print() {
  180.     //  |           System.out.print("hi");
  181.     //  |       }
  182.     //  |    };
  183.     //  |
  184.     //  | Why is that?
  185.     //  | Because of the 'abstract' modifier.
  186.     //  | Abstract means a class cannot be constructed
  187.     //  | as-is. It will need to be extended first.
  188.     //  | Remember above where we talked about
  189.     //  | "Anonymous" classes? The generated
  190.     //  | anonymous classes do exactly this. They
  191.     //  | extend the abstract class which lets us
  192.     //  | use them like seen in the "test" method above.
  193.     public abstract class K {
  194.         public K() {
  195.             System.out.println("ScopeK");
  196.             System.out.println("K");
  197.         }
  198.  
  199.         // ====================================
  200.         //         THE ABSTRACT METHOD
  201.         // ====================================
  202.         //  | Much like an abstract class, these
  203.         //  | require extensions. Refer above to
  204.         //  | the "test" method. You can see that
  205.         //  | we "@Override" the methods, which
  206.         //  | makes it clear that they are extensions
  207.         //  | of this method, overriding their behaviour.
  208.         public abstract void print();
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment