How to Use Constructor with Parameters in Java


In this example we will show you how to use constructor with parameters in Java.

Source Code

package com.beginner.examples;

public class Case {
  String title;
  String content;
  public Case(String str1, String str2) { // constructor with parameters
    title = str1;
    content = str2;
  }

  public static void main(String[] args) {
    Case a = new Case("test", "detail");
    System.out.println(a.title + " " + a.content);
  }
}

Output:

test detail
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments