/ W3SCHOOLS

W3schools - Class/Static

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

OOP(Object Oriented Programming)

Java is OOP language

Procedual programming is about writing procedures or methods that perform operations on the data, while OOP is about creating objects that contain both data and methods

OOP has advantages over procedual programming

  • Faster and easier to execute

  • Provides a clear structure

  • Helps to keep “DRY” and makes the code easier to maintain, modify and debug

  • Makes it possible to create full reusable app with less code and short development time

Class and Object

Two main aspects of OOP

Class is a template for object(is like object constructor)

Object is an instance of a class

  • individual objects are created, they inherit all the variables and methods from the class
Class Object Attribute Method
blueprint volvo weight, color drive,brake

Create Class

public class Main{	// create a class named “Main”
  int x = 5;				// field, class attribute
  int y;
  public static void main(String[] args){
    Main myObj1 = new main();
  //(specify the class name)(object name) = create instance, using keyword “new”
    Main myObj2 = new main();	// can also create more instance
    System.out.print(myObj1.x);	// can access attributes by using the dot
    myObj1.y = 10;			// can modify attribute value
    myObj2.x = 20;			// can also override existing value
    System.out.print(myObj1.y);	// output 10,
    System.out.print(myObj2.x);	// output 20 they are different instance
  }
}

Static

which means that it cans be accessed without creating an object of the class, unlike public

public class Main{
  static void myStaticMethod(){
    System.out.println("blahblah");
  }

  public static void main(String[] args){
    myStaticmethod();
  }
  public static void main(String[] args){
    myStaticMethod();		// Call the static method
    Main myObj = new Main();	// Create an object of Main
    myObj.myPublicMethod();	// Call the public method on the object
}