Classes and methods in java LEARNOVITA

Encapsulation In Java | Complete Tutorial With Examples

Last updated on 24th Aug 2022, Blog, Tutorials

About author

Avinash Tripathi (JavaScript Developer )

Avinash Tripathi has extensive knowledge on Java, Core Java, Spring, J2EE, Struts, and Hibernate. His articles aid students in acquiring domain-specific knowledge.

(5.0) | 19287 Ratings 2205

Classes and Methods in Java

In Java, a technique is nothing but a block of code/statement that’s declared among the category and may perform totally different actions once somebody calls it. Some strategies may be referred to as directly with their name (i.e. while not making the category object) whereas some strategies need an instance/object of the category (i.e. should be invoked with the article of the class).The strategies that may be referred to as a direct area unit are referred to as a category or static strategies, whereas the strategies that require an associate degree object of the category to be invoked are referred to as instance or non-static strategies.

This write-up can gift a close summary of sophistication strategies and during this regard, it’ll cowl the subsequent aspects of Java category methods:

  • What is a category technique ?
  • How to Access category strategies?
  • How to Access Public strategies?
  • How to Access a technique from a distinct category?

Class technique in Java

Generally, after we have a category then we’ve got to make an associate degree object of that class to access its strategies and different members. However,static strategies may be accessed within the category while not making an associate degree instance of that class.

How to Access category strategies

Let’s contemplate the below-given example to know a way to produce and access a static/class technique in Java.

Example

The below code piece takes 2 numbers from the user and perform addition on them:

  • public category AddNumbers come add;
  • }
  • public static void main(String[] args)
  • addition of two numbers

    From the higher than output, it’s clear that there’s no need to produce the article of the category to decide on a static technique; instead , it may be accessed directly at intervals in the category.

    Add Numbers.java

    How to Access Public strategies

    Now let’s contemplate the below example to check whether or not a public technique may be accessed directly or not:

    program for sum of two numbers
  • public category AddNumbers come back add;
  • }
  • public static void main(String[] args) come back add;
  • }
  • }
  • AddNumbers.java
  • public category AddNumbers
  • addition of two numbers

    Although the additional methodology is static, we have a tendency to still get a slip-up once we try to access it directly. This can be as a result of the additional methodology not being within the same category. So, to access the strategy of another category we’ve got to form the thing of that category no matter its access modifier i.e. static or public.

    My Functions.java

    Conclusion:

    The class/static methodology is accessed inside the category directly whereas accessing the general public ways while not making the thing isn’t attainable. While, within the case of multiple categories, the ways are accessible solely with the assistance of sophistication objects despite their access modifier. This write-up provides a comprehensive guide of what square measure category ways and the way to access them from identical categories and from a special category.

    Are you looking training with Right Jobs?

    Contact Us

    Popular Courses