creating a new object in a class 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) {
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
Puppy myPuppy = new Puppy( "tommy" );
}
}
Example 3: using class in java
public class HelloWorld {
public static void main(String[] args) {
class Number{
int number;
public boolean isPos(){
if(number > 0){
return true;
} else {
return false;
}
}
}
Number myNumber = new Number();
myNumber.number = 7;
if(myNumber.isPos()){
System.out.println(myNumber.number + " is positive!!!");
} else {
System.out.println(myNumber.number + " is not positive!!!");
}
}
}
Example 4: using class in java
public class HelloWorld {
public static void main(String[] args) {
class User{
int score;
public boolean hasWon(){
if(score >= 100){
return true;
} else {
return false;
}
}
}
User dave = new User();
dave.score = 20;
System.out.println(dave.hasWon());
}
}