Answers for "object in java"

16

java create new object

Car mycar = new Car();
Posted by: Guest on May-20-2020
11

java class

public class Lightsaber {
  // properties
  private boolean isOn;
  private Color color;
  
  // constructor
  public Lightsaber(Color color) {
    this.isOn = false;
    this.color = color;
  }
  
  // getters
  public Color getColor() {
    return color;
  }
  public boolean getOnStatus() {
    return isOn;
  }
  
  // setters
  public void turnOn() {
    isOn = true;
  }
  public void turnOff() {
    isOn = false;
  }
}



// Implementation in main method:
public class test {
  public static void main(String[] args) {
    Lightsaber yoda = new Lightsaber(green);
    yoda.turnOn();
  }
}
Posted by: Guest on February-18-2020
1

how to make an objec tjava

class Puppy() {
	String name;
	int age;
  
  
 	Puppy(String puppy_name, int puppy_age) {
    	name = puppy_name;
   		age = puppy_age;
 	}
  
  	public static void main(String[] args) {
     	Puppy rex = new Puppy("rex",4); 
    }
  
}
Posted by: Guest on October-09-2020
5

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" );
   }
}
Posted by: Guest on August-29-2020
2

using class in java

public class HelloWorld {
  public static void main(String[] args) {
    
    // how to use class in java
    class User{
    	
    	int score;
    }
    
    User dave = new User();
    
    dave.score = 20;
    
    System.out.println(dave.score);
    
 
  }
}
Posted by: Guest on March-10-2020
0

object in java

Objects: came from class, we can create multiple objects from a class
  ArrayList<>  list     =  new ArrayList<>();
  Class     refName               OBJECT
  
 each object has its own copy of instance variable
· declared outside the blocks or methods
Object: Instance of the class. We can store different data's to objects
Object Creation: with new keyword.     
  ClassName obj = new ExistingConstructor;
Posted by: Guest on May-28-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language