Use the object without static. Method calls a function in Java

Source: Internet
Author: User

Sir, as an object, call it in the "object. Method ()" way.
The main method in Java is static, used for the entry of the program, cannot be called in a static method, only static methods can be called. If you want to invoke a static method, you will be the object of the class, calling the non-static method through the object.
such as:
public class Squareinttest {

public static void Main (string[] args) {
int result;
squareinttest m = new Squareinttest ();
for (int x = 1; x <= × x + +) {
result = (int) m.square (x);
The Math library also provides a way to calculate the square number
//result= (int) Math.pow (x,2);
System.out.println ("The square of" + x + "is" + result + "\ n");
}
}

A static method for calculating the square number of a custom
public int Square (int a)
{
return a*a;
}
}

Use the object without static. Method calls a function in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.