how to create a object in java code example
Example 1: java create new object
Car mycar = new Car();
Example 2: make an object in java
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
Example 3: using class in java
public class HelloWorld {
public static void main(String[] args) {
// how to use class in java
class User{
int score;
// method that can be used outside the class
public boolean hasWon(){
if(score >= 100){
return true;
} else {
return false;
}
}
}
User dave = new User();
dave.score = 20;
System.out.println(dave.hasWon());
}
}