Muscle Java Object Oriented Day 1

instance

What is a class? (Review) Something like a design document ・ What to define -Status (status): Variable (field) -Behavior (behavior): method

--Entity created from design document (class) --Multiple generation is possible

インスタンス

Instance (code generation)

Class name instance variable name(freedom) =new class name(argument)[Constructor specification];

Store the generated instance in a variable Notable here! !! __ Class type (reference type) instance variable __

All Java variables are statically typed to declare class types

constructor

--A special method that performs initialization processing when creating an instance --Constructor features --The method name is the same as the class name --No return value --There may be multiple constructors with different arguments --Constructor is optional

//Person class

public class Person {
  public Person(){
   age = 1;
  }
  public Person(int a){
   age = a;
  }
  public Person(String a){
   age = a;
  }
  public void printAge(){
   System.out.println("I"+age+"I'm old");
  }
 }
}
//main class

public static void main(String args[]){
  Person p1 = new Person();
  p1.printAge();
  
  int num1 = 29;
  Person p2 = new Person(num1){
  p2.printAge();
  }

  String num2 = "29";
  Person p3 = new Person(num2){
  p3.printAge();
  }
}

static definition

Access to static fields and static methods --Can be used without instantiating fields and methods with static keywords

Contents How to use
Use static method name of the class.Method name(argument)
Use static fields name of the class.Field name

Static definition restrictions

--static field --Only one variable value can be held → Overwritten each time it is stored --static method --You cannot use methods without static or fields without static in the same class from within static methods. --Accessible by creating an instance in a static method

Class cooperation

--When using classes from other packages --Specify directly in the code with "package name.class name" --Used in import declaration --import Other package name. Class name; --import package name. * (All classes);

Recommended Posts

Muscle Java Object Oriented Day 1
Muscle Java Object Oriented Day 2 ~ Inheritance ~
Muscle Java Basics Day 1
Java Day 2018
Java learning day 5
Java Day Tokyo 2017
Oracle Java Day 2017
Oracle Java Day 2017
[Java] Object class
java learning day 2
java learning day 1
JAVA object mapping library
Java Silver Study Day 1
Java Kuche Day memo
Java object size feeling
[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
Java Object Serialization why & when
Maybe this is object oriented
4th day of java learning
Java is the 5th day
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]
[Effective Java] Remove obsolete object references
[Technical Note] What is Object Oriented?