Saturday, January 12, 2019

Java : How to create an object


public class HelloWorld{
public static void main (String[] args){

System.out.println("Begining of my Hello World");
World worldOne = new World("Jacky World"); // Creating the object of the 'world.java' class
worldOne.printName(); // calling the method from 'world.java'
}
}
public class World{

public String name;

public World(String name){
this.name = name;
}

public void printName(){
System.out.println("My World's name is " + this.name);
}
}

No comments:

Post a Comment