Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

constructor

public class ConsDemo {
   public static void main(String args[]) {
      MyClass t1 = new MyClass();
      MyClass t2 = new MyClass();
      System.out.println(t1.num + " " + t2.num);
   }
}
Comment

constructor

class Person {
    var children: MutableList<Person> = mutableListOf<>()
    constructor(parent: Person) {
        parent.children.add(this)
    }
}
Comment

Constructor


class MyMainEmployee{
    private int id;
    private String name;

    public MyMainEmployee(){
        id = 0;
        name = "Your-Name-Here";
    }
    public MyMainEmployee(String myName, int myId){
        id = myId;
        name = myName;
    }
    public MyMainEmployee(String myName){
        id = 1;
        name = myName;
    }
    public String getName(){
        return name;
    }
    public void setName(String n){
        this.name = n;
    }
    public void setId(int i){
        this.id = i;
    }
    public int getId(){
        return id;
    }
}

public class cwh_42_constructors {
    public static void main(String[] args) {
        //MyMainEmployee d = new MyMainEmployee("ProgrammingWithHarry", 12);
        MyMainEmployee d = new MyMainEmployee();
        //d.setName("Learn java");
        //d.setId(34);
        System.out.println(d.getId());
        System.out.println(d.getName());
    }
}
Comment

constructor definition

Constructor are a type of method 
* Share the name of class
* Called when you Instantiate a Class

Default Constructor accepts no Arguments
Comment

constructer

class Ball {
	constructor(w, h) {
		this.width = 100;
      this.height = 100;
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Exceptions - Try...Catch 
Java :: Error:java: error reading C:UsersMeriem.m2 epositoryorgapachecommonscommons-lang33.12.0commons-lang3-3.12.0.jar; error in opening zip file 
Java :: Using multiple delimiters with String Tokenizer 
Java :: java convert ip to long 
Java :: How to merge two sorted arrays into a single sorted aggregate one? 
Java :: java anonmyous array eg 
Java :: initialize mocks 
Java :: an internal error occurred during initializing java tooling . java.lang.nullpointerexception 
Java :: Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main 
Java :: long to double in java 
Java :: exit an if statement java 
Java :: get free player inventory slots spigot 
Java :: type variable java 
Java :: interface java 
Java :: springbootservletinitializer maven dependency 
Java :: using java stream filter to find the sum of property 
Java :: add close button on right top corner of alert dialog box for android 
Java :: check if LinkedList is empyth java 
Java :: set union java 
Java :: java array object 
Java :: java hashmap remove by condition 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: update in spring boot 
Java :: android send fragment context 
Java :: how to call a static method in java 
Java :: Android listen to network change 
Java :: firebase realtime database get key 
Java :: How to change integers into float in java 
Java :: java int 0/0 
Java :: how to convert double to int in java 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =