Kantai Collection Java # 1 Classes and Objects [For Beginners]

Introduction

Java is an object-oriented programming language. Everything in Java is related to classes and objects along with attributes and methods. For example, destroyers have attributes such as firepower and anti-submarine value, and have methods such as anti-submarine attack.

Create a class

Use the keyword class when creating a class.

Battleship.java


public class Battleship {
  int x = 3;
}

The class name must start with a capital letter and the class name and file name must match. In this case, ** Battleship ** in ** Battleship **. Java and ** Battleship ** in the class name match.

Create an object

In Java, you create an object from a class. When creating an object, write the class name after new.

Battleship.java


public class Battleship {
  int x = 3;

public static void main(String[] args) {
  Battleship yamato = new Battleship(); //Create an object called yamato in the Battleship class
  System.out.println(yamato.x); //Output the value of x
  }
}

Create multiple objects

You can also create multiple objects in one class.

Battleship.java


public class Battleship {
  int x = 3;

public static void main(String[] args) {
  Battleship yamato = new Battleship(); //Create an object called yamato in the Battleship class
  Battleship musashi = new Battleship(); //Create an object called musashi in the Battleship class
  System.out.println(yamato.x); //Output the value of x
  System.out.println(musashi.x); //Output the value of x
  }
}

Recommended Posts

Kantai Collection Java # 1 Classes and Objects [For Beginners]
Classes and instances Java for beginners
[For beginners] Explanation of classes, instances, and statics in Java
[For beginners] Difference between Java and Kotlin
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]
java classes, instances, objects
A collection of simple questions for Java beginners
java (classes and instances)
List of frequently used Java instructions (for beginners and beginners)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[Java] Generics classes and generics methods
Java for beginners, data hiding
Java abstract methods and classes
Java application for beginners: stream
Java while and for statements
Java, abstract classes starting from beginners
AWS SDK for Java 1.11.x and 2.x
Java generics (defines classes and methods)
[For Java beginners] About exception handling
Convert Java org.w3c.dom.Document objects and XML strings
(For beginners) Swift UI view element collection
Learn Java with "So What" [For beginners]
☾ Java / Collection
[Java] for Each and sorted in Lambda
Sample code collection for Azure Java development
Java9 collection
[Java] Collection and StringBuilder operation method comparison
JAVA learning history abstract classes and methods
[Java] Proxy for logging SQL and SQL results
Comparison of JavaScript objects and Ruby classes
[Java ~ Classes and External Libraries ~] Study Memo (6)
[Java] Contents of Collection interface and List interface
Review notes for Java 1.7 and later file copies
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
[Java] Personal summary of classes and methods (basic)
Differences in writing Java, C # and Javascript classes
[For beginners] About lambda expressions and Stream API
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
Classes and instances
For JAVA learning (2018-03-16-01)
[Member and collection]
Java Reintroduction-Java Collection
2017 IDE for Java
Java and JavaScript
XXE and Java
Java for statement
[Java] Collection framework
About products using crud processing and devise (for beginners)
Sample (Java) for OAuth 2.0 authentication and access token acquisition
[For beginners] DI ~ The basics of DI and DI in Spring ~
VSCode Java Debugger for Java Build failed Causes and countermeasures
[For beginners] Minimum sample to display RecyclerView in Java
Get Locale objects for all locales available in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
Learn for the first time java # 3 expressions and operators
Java classes and instances to understand in the figure
Introduction to Java for beginners Basic knowledge of Java language ①
Array variables and associative arrays compiled by beginners for beginners
This and that for editing ini in Java. : inieditor-java