How To Call A Method

Table of contents:

How To Call A Method
How To Call A Method

Video: How To Call A Method

Video: How To Call A Method
Video: Beginner Java Tutorial #5 Declaring and Calling Methods in Java 2024, May
Anonim

Object-oriented methodology greatly facilitates the programming process. The classes used within its framework and their instances - objects, expand the possibilities for solving any problem. Class functions that describe all kinds of object behavior are called methods. Depending on the modifier specified during the development of the class (public, protected, private), access to its methods can be carried out in different ways. The point of the function call is also of great importance here.

How to call a method
How to call a method

Instructions

Step 1

Any time you refer to a class, you should consider the scope of its visibility. It is advisable to indicate the file with the description of the class at the beginning of the program code. To do this, write a construction like #include “File_name.h”. Or insert the description code itself in the same place. Before calling the method, initialize the object using the following notation: CClass1 Obj1, here CClass1 is the class name, Obj1 is the name of the object. Along with a class object, pointers to its instance can also be used. In this case, declare a pointer and allocate memory: CClass1 * Obj2 = new CClass1 ().

Step 2

Call the method of the object with the following command: Obj1.metod1 (), here the operator "." (dot). When working with a pointer to an instance of a class, use the "->" operator: Obj2-> metod1 (). Consider the scope of the object or pointer. So, when declaring a variable within one function, it will not be visible to the compiler outside of it.

Step 3

If a class method is described with a public access specifier, it can be called using the above methods from anywhere in the program. However, often methods, in order to protect data, receive the status of hidden. So, when declared using private, a function can only be available inside its class. It is called only within the framework of another method of an instance of the same class. The protected modifier also disables the use of the method for third-party code, but provides such an opportunity for child classes. An example of calling a method in an inherited class: class A // parent class {protected: void funcA ();}; class B: public A // inherited (child) class {public: void funcB () {funcA (); } // call a method of the parent class};

Step 4

When accessing a method of a class in another function of the same class, it is unnecessary to create an instance of it. It is enough to specify the name of the method and the parameters to be passed. An example of the method call code: class CClass2 {void func1 (int k); void func2 () {func1 (50); }};

Step 5

There is another way to access the method without instantiating the class. However, this requires that the specified method be declared in the class as static. An example of the method description in the class: class CClass3 {static int func3 ();} In this case, the call to the func3 method can be performed anywhere in the program using the construction: CClass3:: func3 ().

Recommended: