ABOUT ME

Today
Yesterday
Total
  • Class : Introduction
    Java 2022. 2. 3. 18:26

    자바의 클래스에 대한 개념 및 문법을 정리하였습니다.

     

     


    < Introduction to Classes >

    Class are created to make a entity for the world.
    For example, a program to track student test scores might have Student, Course, and Grade classes.

    An instance of class is called Object. Object have state(information) and method(behavior)
    For example, each student of the Student class is object.

    This is object-oriented programming because programs are built around objects and their interactions. 

     

    • Classes are a blueprint for objects. Blueprints detail the general structure.
      For example, all students have an ID, all courses can enroll a student, etc.

     

     


    < Classes: Syntax >

    class is the set of instructions that describe how an instance can behave and what information it contains.

     

    < Syntax >
    public class ClassName {
    // scope of the class starts after curly brace
     
      public static void main(String[] args) {
        // scope of main() starts after curly brace
     
        // program tasks
     
      }
      // scope of main() ends after curly brace
     
    }
    // scope of the class ends after curly brace

    - public is an access level modifier that allows other classes to interact with this class.
    - main( ) lists tasks performed by the program. main( ) runs when execute the file.

     

     


    < Classes: Constructors >

    In order to create an object (an instance of a class), we need a constructor method. The constructor is defined within the class.

     

    < Syntax >
    public class Car {
     
      public Car() {
        // instructions for creating a Car instance
      }
     
      public static void main(String[] args) {
        // Invoke the constructor
        Car ferrari = new Car(); 
      }
    }​

    - The constructor, Car( ), shares the same name as the class.
    - To create an instance, we need to call or invoke the constructor within main( ) :
    1.
    with the class name (variable's type name).
    2.
    use the keyword new to indicate taht we're creating an instance. Omitting new causes an error.

     

    Example)

    public class Store {
      public Store() {
        System.out.println("I am inside the constructor method.");
      }
      public static void main(String[] args) {
        System.out.println("Start of the main method.");
        Store lemonadeStand = new Store();
        System.out.println(lemonadeStand);
      }
    }
    
    // Start of the main method.
    // I am inside the constructor method.
    // Store@2aae9190

    : Variable lemonadeStand is declared as a reference data type. This means that the value of our variable is a reference to an instance’s memory address.

    : In Store@2aae9190, Store refers to the class, the @2aae9190 refers to the instance's location in the computer's memory.

     

     


    < Classes: Instance Fields >

    To add data to an object by using instance variables, or instance fields.

    Instance variables are the nouns and adjectives associated with an object.

     

    Example)

    public class Car {
      /*
      declare fields inside the class
      by specifying the type and name
      */
      String color;
      public Car() {
      }
      public static void main(String[] args) {
      }
    }

    : The declaration is within the class and the instance variable will be available for assignment inside the constructor.

     

     


    < Classes: Constructor Parameters >

    To create objects with dynamic, individual states, use a combination of the constructor method and instance fields.

     

    < Syntax >
    public class Car {
      String color;
      public Car(String carColor) {
        color = carColor;
      }
      public static void main(String[] args) {
      }
    }​

    - To assign a value to an instance variable, need to alter our constructor method to include parameters so that is can access the data we want to assign to an instance.
    - When a String value gets passed into Car( ), it is assigned to the parameter carColor. (These values are referred to as arguments)
    - The type of the value given to the invocation must match the type declared by the parameter.
    - To access the object's state, use Dot operator ' . ' 

     

     

    Example 1 : Single Instance Field

    public class Store {
      // instance fields
      String productType;
      
      // constructor method
      public Store(String product) {
        productType = product;
      }
      
      // main method
      public static void main(String[] args) {
        Store lemonadeStand = new Store("lemonade");
        System.out.println(lemonadeStand.productType);    
      }
    }

    Example 2 : Multiple Instance Fields

    public class Car {
      String color;
      boolean isRunning;
      int velocity;
     
      public Car(String carColor, boolean carRunning, int milesPerHour) {
        color = carColor;
        isRunning = carRunning;
        velocity = milesPerHour;
      }
     
      public static void main(String[] args) {
        Car ferrari = new Car("red", true, 27);
        Car renault = new Car("blue", false, 70);
     
        System.out.println(renault.isRunning);
        // false
        System.out.println(ferrari.velocity);
        // 27
      }
    }

    +. Ordering matters! We must pass values into the constructor invocation in the same order that they’re listed in the parameters.

    // values match types, no error
    Car honda = new Car("green", false, 0);
     
    // values do not match types, error!
    Car junker = new Car(true, 42, "brown");

     

     


    plus : multiple constructors

    In Java, because of constructor overloading, a class can have multiple constructors as long as they have different parameter values.

    The Signature(constructor's name and parameters) is useful in that it helps the compiler differentiate between the different method.

     

     

    Example)

    public class Car {
      String color;
      int mpg;
      boolean isElectric;
     
      // constructor 1
      public Car(String carColor, int milesPerGallon) {
        color = carColor;
        mpg = milesPerGallon;
      }
      // constructor 2
      public Car(boolean electricCar, int milesPerGallon) {
        isElectric = electricCar;
        mpg = milesPerGallon;
      }
    }

    : When we initialize an object, the compiler will know which constructor to use because of the values we pass into it. For example, Car myCar = new Car(true, 40) will be created by the second constructor because the arguments match the type and order of the second constructor’s signature.

     

    • If we do not define a constructor, the Java compiler will generate a default constructor that contains no arguments and assigns the object default values. Default values can be created by assigning values to the instance fields during their declaration:

    Example)

    public class Car {
      String color = "red";
      boolean isElectric = false;
      int cupHolders = 4;
     
      public static void main(String[] args) {
        Car myCar = new Car();
        System.out.println(myCar.color); // Prints: red
      }

     

     

    Refernence : Java Overloading

     

    [java]자바 강의 26. "오버로딩(Overloading)"이란? 생성자(Constructor) 오버로딩에 대하여

    오버로딩(Overloading) : 과부하, 많이 싣는다오버로딩(Overloading) 우리는 지난 시간 생성자의 정의와 ...

    blog.naver.com

     

     

     

     

    'Java' 카테고리의 다른 글

    Conditional  (0) 2022.02.05
    Class : Method  (0) 2022.02.04
    Variables : Manipulating  (0) 2022.02.01
    Variables  (0) 2022.01.31
    IDE  (0) 2022.01.30

    댓글

Designed by Tistory.