Menu
×
×
Correct!

Exercise:

Create and call a class constructor of MyClass
Follow the comments to insert the missing parts of the code below:

// Create a MyClass class public class MyClass { int x; // Create a class attribute x // Create a class constructor for the MyClass class public MyClass() { x = 5; // Set the initial value for the class attribute x to 5 } public static void main(String[] args) { // Create an myObj object of class MyClass (This will call the constructor) MyClass myObj = new MyClass(); // Print the value of x System.out.println(myObj.x); } }

Not Correct

Click here to try again.

Correct!

Next ❯
// Create a MyClass class
public class  {
  int ;  // Create a class attribute x

  // Create a class constructor for the MyClass class
  public () {
    x = ;  // Set the initial value for the class attribute x to 5
  }

  public static void main(String[] args) {
    // Create an myObj object of class MyClass (This will call the constructor)
    MyClass  = new MyClass(); 
    // Print the value of x
    System.out.println(.);
  }
}

  
  
  




Go to w3schools.com
Reset Score
Close This Menu

 

Java Syntax
Java Variables
Java Data Types
Java Operators
Java Strings
Java Math
Java Booleans
Java If...Else
Java Switch
Java Loops
Java Arrays
Java Methods
Java Classes/Objects
Java Exceptions





×

Reset the Score?

This will reset the score of ALL 59 exercises.

Are you sure you want to continue?



×

Congratulations!

You have finished all 59 Java exercises.

Share your score on facebook: