Join Regular Classroom : Visit ClassroomTech

Programming in JAVA – codewindow.in

Related Topics

JAVA Programming

public class MyClass {
    private int myInt;   // private variable
    public String myString;  // public variable
    double myDouble;   // default access variable

    // other class components here
}
  1. Methods: Methods define the behavior or actions that a class can perform. They can be of different types, including instance methods (which operate on an instance of the class), static methods (which operate on the class itself), or constructors (which are used to create new objects of the class). Like variables, methods can also have different access modifiers, which determine their visibility and accessibility.

Here is an example of defining methods in a Java class:

public class MyClass {
    private int myInt;
    public String myString;
    double myDouble;

    public void myMethod() {    // instance method
        // method body here
    }

    public static void myStaticMethod() {   // static method
        // method body here
    }

    public MyClass(int i, String s, double d) {   // constructor
        myInt = i;
        myString = s;
        myDouble = d;
    }
}
  1. Constructors: Constructors are special methods that are used to create new objects of the class. They are called when a new instance of the class is created using the “new” keyword. Constructors can take arguments or parameters, which are used to initialize the variables of the class.

Here is an example of defining a constructor in a Java class:

public class MyClass {
    private int myInt;
    public String myString;
    double myDouble;

    public MyClass(int i, String s, double d) {   // constructor
        myInt = i;
        myString = s;
        myDouble = d;
    }

    // other class components here
}
[access-modifier] class ClassName {
    // class variables, methods, and constructors here
}

Let’s break down the syntax:

  • [access-modifier]: This is an optional access modifier, such as public, private, protected, or default (no modifier specified). It determines the visibility of the class within and outside of the package.

  • class: This is a Java keyword that indicates that you are declaring a class.

  • ClassName: This is the name of the class, which should be a valid Java identifier and should start with an uppercase letter.

  • {}: These curly braces enclose the body of the class, which contains the class variables, methods, and constructors.

Here is an example of declaring a simple class in Java:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

In this example, we have declared a class called Person with two private variables (name and age), a constructor that initializes these variables, and a public method (sayHello) that prints a message to the console. The class has a public access modifier, which means that it can be accessed from outside of the package.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories