← Back to feed
Renewal·서른의 생활코딩

ex1) java_oop

NS
normalstory
cover image


//public class Date {  

class DateClass {  // because the main class is inside a single source file...
        // before `class`, only `public` or omission is allowed as the access modifier
        // `class`: reserved word for creating a data type (written in lowercase)
        // `DateClass`: class name = user-defined name
        // `{` = start of the Date class (set / get)
 
// int year,month,day; // original example
         // before `int`, an access modifier such as `public, private, protect,` or omitted can be used
         // = DateClass member variable (= data member)
         // this becomes a memory space item when an object is created with `new`.
 
         private int year,month,day; // "[a] example"
 
 
         public DateClass(){ // constructor scope |->
              // = constructor
              // before `DateClass()`, one of `public, private, protect,` or omitted may be used
              // the constructor name `DateClass()` must exactly match the class name
              // a constructor name can never have a return type such as `void` in front of it
              // a constructor cannot be called arbitrarily from outside
              // it is called automatically when the object is created
              // and simply initializes the defined object
              // a constructor cannot contain a `return` statement.

          System.out.println("Running default constructor");

          year=2012;
          month=02;
          day=15;
          }
 
          public DateClass(int y, int m, int d){
          // = overloaded function
          // constructors can be written multiple times as overloads
          // Java does not have a destructor
          // if two are defined at the same time, the constructor with initialized values is called first

          System.out.print("Running constructor with three parameters");
          year=y;
          month=m;
          day=d;
          }
          // up to here is the constructor scope ->|
          // if no constructor exists at all,
          // the JVM creates and uses a default constructor
          // (for example, global variables are also initialized automatically.)

 
 
 
 

// |-> "member functions" (= methods) = describe how processing should be handled!!!
// a pathway for arbitrary access later on!!!
// since getYear() returns a value, a return type must be written!!!
    public int getYear(){   // e.g. not just `public getYear(){`
          // `public` = access modifier
          // `int` is the return type!!!
          // `getYear` = member function name
       return year;
    } 
    public int getMonth(){
       return month;
    }
    public int getDay(){
       return day;
    }
 
 
 
 
   // functions related to changing values: set
   public void setYear(int y){ // `int y` to receive a value
      year=y;
     // only changing a value, so no `return` used
   }
   public void setMonth(int m){ // `int m` to receive a value
      month=m;

   }
   public void setDay(int d){ // `int d` to receive a value
      day=d;
   }
 
 
 
   public void setDate(int y,int m, int d){
      year=y;
      month=m;
      day=d;
   }
 
   // print the values in the object!!!
   public void Display(){
      System.out.print(year + "-" + month + "-" + day);
   }
} 

 

 

public class Date { 
 public static void main(String str[]){
  
  DateClass d1=null; // example that creates `d1` in stack memory and sets it to null
  //d1.Display(); // calling through a null variable
  // NullPointerException error
  // = when trying to access (call) a variable in a null state
  //DateMain(); // DateMain is only a constructor
  // first an object must be created.
  d1 = new DateClass();
  d1.Display();    // works here (it is no longer null)
  // object creation => memory is allocated on the heap
  // when the object is created, it is first created by `object` even without a constructor
  // then, if Date members exist,
  // a function table is created and shared
  // this becomes one unit of processing
  // after that, an address value goes into `d1`.
  // because `DateClass()` has empty parentheses,
  // it automatically enters the public constructor
  // and then execution continues below it.
  // the constructor itself cannot be accessed directly.
  DateClass d2 = new DateClass(1991,5,16);
  d2.Display(); // because of the implicit `this`, it prints a different value from `d1.Display()`
  // this:
  // 1) is not explicitly allocated by the programmer; the compiler creates it internally
  // 2) when there are several members like year, month, day inside member functions,
  // it distinguishes which object's member they belong to
  // 3) the address value of `d2` is automatically passed through `this`
  // the compiler automatically gives the object's address to the auto-generated `this`
  // and when referring to member variables inside constructors or methods, `this.` is understood implicitly.
  //d2.year=2011; // cannot be accessed because it is out of scope and private
  // if you still want to change it, that is the core role of a member function: change what you want while protecting the field
  d2.setYear(2011); // this is also understood automatically as `this.year`
  // System.out.println(d2.year); // cannot retrieve it directly because `year` is private
  // if you still want to print it, call `getYear()`!!!
  System.out.println("This is d2's year =" +d2.getYear());
 } }

This English version was translated by Codex.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.