how to create object in java with 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 User{
int score;
}
User dave = new User();
dave.score = 20;
System.out.println(dave.score);
}
}
Example 4: using class in java
class Number {
int number;
public boolean isSquare(){
double squareRoot = Math.sqrt(number);
if(squareRoot == Math.floor(squareRoot))
{
return true;
} else {
return false;
}
}
public boolean isTriangular(){
int x = 1;
int triangularNumber = 1;
while(triangularNumber < number){
x++;
triangularNumber = triangularNumber + x;
}
if(triangularNumber == number){
return true;
} else {
return false;
}
}
}
Number myNumber = new Number();
myNumber.number = 16;
System.out.println(myNumber.isSquare());