Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java Constructor Overloading

class Main {

  String fruit;

  // constructor with no parameter
  Main() {
    this.fruit = "Banana";
  }

  // constructor with a single parameter
  Main(String fruit) {
    this.fruit = fruit;
  }

  public void getName() {
    System.out.println("Fruit: " + this.fruit);
  }

  public static void main(String[] args) {

    // call constructor with no parameter
    Main obj1 = new Main();

    // call constructor with a single parameter
    Main obj2 = new Main("Apple");

    obj1.getName();
    obj2.getName();
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Access items from the ArrayList using get() function 
Java :: java stream loop 
Java :: spring boot MVC config 
Java :: raise error java 
Java :: add two numbers bitwise 
Java :: java reverse string 
Java :: one line if statement java 
Java :: set solid color background android programatically in drawable 
Java :: java random double between 0 and 1 
Java :: android toast 
Java :: center textview programmatically android 
Java :: add character to a string java 
Java :: string array to stringbuilder array java 
Java :: array contain java 
Java :: palindrome java 
Java :: char to ascii java 
Java :: how to break switch case in java 
Java :: how to create a gui in java 
Java :: android set focus on edittext 
Java :: fixed length array powershell 
Java :: what is a do while loop in java 
Java :: override class java 
Java :: Cannot find a class with the main method. 
Java :: compareto in java string 
Java :: iterate list in java 
Java :: icon share android 
Java :: how to create xml file in java 
Java :: how to concatenate two strings in java 
Java :: java comparing-two-csv-files-in-java 
Java :: java set operations 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =