0% found this document useful (0 votes)
1 views11 pages

Lesson 2: Object-Oriented Programming

This lesson covers the basics of Object-Oriented Programming, focusing on methods and attributes, specifically constructors, class methods, and variables. It explains how to declare and call static methods within a class, providing examples of syntax and usage. The document illustrates the difference between static and instance variables through code examples.

Uploaded by

tongquin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views11 pages

Lesson 2: Object-Oriented Programming

This lesson covers the basics of Object-Oriented Programming, focusing on methods and attributes, specifically constructors, class methods, and variables. It explains how to declare and call static methods within a class, providing examples of syntax and usage. The document illustrates the difference between static and instance variables through code examples.

Uploaded by

tongquin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lesson 2

Object-Oriented
Programming
Methods and Attributes
Agenda Constructor
Class Methods and
Variables
Methods are declared within a
Method class, and that they are used to
perform certain actions.
Example:

Create a method named myMethod() in Main:

public class Main {


static void myMethod() {
System.out.println("Hello World!");
}
}
myMethod() prints a text (the action), when it
is called. To call a method, write the method's
name followed by two parentheses () and a
semicolon;
Example:
Inside main, call myMethod():
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}

public static void main(String[] args) {


myMethod();
}
}
The static keyword is used to

Static and
construct methods that will exist
regardless of whether or not any

Public instances of the class are generated.


Any method that uses the static
keyword is referred to as a static
method.
Syntax to declare the static method::

Access_modifier static void methodName()


{
// Method body
}
Syntax to call a static method:

className.methodName();
public class GFG {
// static variable
static int a = 40;

// instance variable
int b = 50;

void simpleDisplay()
{
System.out.println(a);
System.out.println(b);
}
// Declaration of a static method.
static void staticDisplay()
{
System.out.println(a);
}

// main method
public static void main(String[] args)
{
GFG obj = new GFG();
obj.simpleDisplay();

// Calling static method.


staticDisplay();
}
}

You might also like