DEV Community

Jack Pritom Soren
Jack Pritom Soren

Posted on

5 2

Method and Attribute in OOP

Method :

A method is the equivalent of a function in object-oriented programming. A noun is to a verb what a variable is to a method — the methods are the actions that perform operations on a variable. A method accepts parameters as arguments, manipulates these, and then produces an output when the method is called on an object. Methods are similar to functions, but methods are also classified according to their purpose in the class design. In classes, variables are called attributes, so methods often operate on attributes.

public class Main {
  static void myMethod() { //myMethod() is the name of the method
    // code to be executed
  }
}
Enter fullscreen mode Exit fullscreen mode

Calling Method :

public class Main {
  static void myMethod() {
    System.out.println("From Method");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

// Outputs "From Method"
Enter fullscreen mode Exit fullscreen mode

Attribute :

Attributes are data members inside a class or an object that represent the different features of the class. They can also be referred to as characteristics of the class that can be accessed from other objects or differentiate a class from other classes.

public class Main {
  int x = 1;
  int y = 2; 
  // Create a class called "Main" with two attributes: x and y
}
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay