Classes

invisibleufo101·2024년 9월 14일

Java

목록 보기
3/10
post-thumbnail

Class Members

  • Fields: Properties or characteristics of an object.
    • Example for Human: age, height, weight, eyeColor, etc.
    • Example for Dog: breed, numberOfLegs, etc.
    • Example for Car: brand, modelName, modelNumber, etc.
  • Methods: Actions that an object can perform.
    • Example for Human: run(), walk(), eat(), talk()
    • Example for Dog: bark(), walk(), run(), sit()
    • Example for Car: startEngine(), accelerate(), brake()

Syntax for fields and methods:

  • .value represents a field.
  • .add() represents a method.

Further Details

Fields

  • Fields are unique variables declared in a class and can be used by any methods in that class.

    public class Foo {
        int bar; // Field
        
        Foo() {
            // Constructor
            this.bar = bar;
        }
    
        public static void someMethod() {
            // Method
        }
    }

Fields vs Variables

  • A field is different from a variable in that it is tied to a class.

  • Fields are initialized when they are declared.

    • Example:
    int intField;      // Initialized to 0
    String strField;   // Initialized to null
    float floatField;  // Initialized to 0.0f

Types of Variables in Java

  1. Class Variables: Usually declared with static and are shared by all members of a class.
  2. Instance Variables: Declared in a class and accessible by instances of that class.
  3. Local Variables: Declared within methods and only accessible within the method scope.

In the scope of fields, we only deal with class variables and instance variables.


Further Detail

1. Class Variables

  • Declared using the static keyword. These variables are shared across all instances of the class.
  • Exists for the lifetime of the program and is common to all objects created from the class.

Ex)

public class SomeClass {
    public static int cnt = 0;
    
    public static void increaseCnt(){
    	cnt++;
   }
}

public class Example {
	public static void main(String[] args){
	  SomeClass someClass1 = new SomeClass();
      SomeClass someclass2 = new SomeClass();
      
      someClass1.increaseCnt();
      
      System.out.println(someClass1.cnt); // 1
      System.out.println(someClass2.cnt); // 1
  }
}

We can see that even though someClass2 didn't call increaseCnt() method, it still prints 1.

2. Instance Variables

  • Declared without the static keyword. Each instance (object) of the class has its own individual state.

Ex)

public class SomeClass {
    private String someField;  // Instance variable
    
    public SomeClass(String someField) {
        this.someField = someField;
    }
}

Each SomeClass object will have its own copy of someField.

3. Local Variables

  • Declared within a method or block and used only within that method or block. Its lifecycle is ephermal as it lives only within the method or block it’s declared in and is destroyed once the method finishes executing.

Ex)

public class SomeClass {
    public void someMethod() {
        int temp = 90;  // Local variable
        System.out.println("Temp Value: " + temp);
    }
    
    public void anotherMethod(){
    	System.out.println(temp); // Compiler Error!
    }
    
}

temp is a local variable that only exists during the execution of the someMethod. Trying to access the temp variable in another method will cause a compiler error because it’s out of scope in that method.


Declaring a Class

(!) The Java file name must match the class name.

// Foo.java

public class Foo {

}

The new Operator

Used to create a new instance of a class.

ClassName objectName = new ClassName();

OR

ClassName objectName = null;
objectName = new ClassName();

Class vs Instance

  • A class is like a blueprint or template used to create an object (instance).

  • An instance is the actual object created from the class.

    • Example:
    Car brandNewCar = new Car("brandName", "modelName", "licensePlateNumber");

    The new operator creates a new object in the Heap Area of memory.

profile
하나씩 차근차근

0개의 댓글