Java 中静态方法和非静态方法的区别
静态和非静态方法是理解的基本方法。本文讨论了它们的属性、逻辑流程和整体实现。
最初,我们将在静态方法上运行一个示例;然后,我们将展示一个关于非静态的简单演示。最后,我们将在一个表中对两者进行分类,并在单个代码块中使用它们。
Java 中的静态方法
在 Java 中,static
是一个特殊的保留关键字,它允许方法在没有类实例的情况下运行。
由于静态
方法是类的一部分,我们不需要创建实例。让我们直接进入正题;查看下面的代码。
代码:
//This demo shows how you can use static methods!
public class StaticMethodDemo {
// We define static keyword to use static method
static void demoStaticMethod() {
System.out.println("We can call static methods without having to create their objects");
}
// Function
public static void main(String[] args) {
demoStaticMethod(); // This calls our static method
}
}
输出:
We can call static methods without having to create their objects
Java 中的非静态方法
static
关键字不是命名非静态方法或变量所必需的。它也对应一个类对象;因此,你需要构造该类的一个实例才能访问它。
代码:
class NonStaticMethodDemo
{
public void NonStaticDemo()
{
System.out.println("This is a demonstration of a Non Static Method in Java");
}
public static void main(String agrs[])
{
NonStaticMethodDemo NonSM=new NonStaticMethodDemo();
NonSM.NonStaticDemo();
}
}
输出:
This is a demonstration of a Non Static Method in Java
在前面的示例中,我们不必将对象调用到 static
方法。
相反,不能直接调用非静态方法。它会抛出编译错误。
检查以下示例中的差异。
Java 中静态方法和非静态方法的区别
静态方法不需要实例变量;否则,你将收到编译错误。相反,你可以使用实例变量调用非静态方法。
差异表:
属性 | 静态 | 非静态 |
---|---|---|
可访问性 | 仅访问静态方法 | 它可以同时访问 |
声明 | 我们只用 static 关键字声明它 |
不需要特殊关键字 |
内存 | 更少的内存(内存分配发生一次) | 更多内存 |
绑定 | 使用编译时绑定 | 运行时间绑定/动态绑定 |
语法 | static void DemoStatic() {} |
void IamNonStaticDemo() {} |
Java 中具有静态和非静态方法的代码的实现步骤
- 类 -
public class DifferenceBtwStaticVsNonStaticDemo {}
- 静态与非静态变量 -
String demo = "Variable";
static String var = "Static Varibale";
- 实例 -
DifferenceBtwStaticVsNonStaticDemo 实例 = new DifferenceBtwStaticVsNonStaticDemo();
。我们将使用这个实例来调用IamNonStaticDemo
方法。 - 静态方法不需要实例 -
DifferenceBtwStaticVsNonStaticDemo.DemoStatic();
现在,检查以下程序。
代码:
public class DifferenceBtwStaticVsNonStaticDemo { //main class
//we will call these variables in both methods
String demo = "Variable";
static String var = "Static Variable";
//main function
public static void main (String[] args) {
//Your code goes here
//Creating Instance of the main class
DifferenceBtwStaticVsNonStaticDemo instance = new DifferenceBtwStaticVsNonStaticDemo();
//instance will call a non static method
instance.IamNonStaticDemo();
//Static methods do not need instance variables
DifferenceBtwStaticVsNonStaticDemo.DemoStatic();
}
//This is static method
static void DemoStatic() {
System.out.println("Static Method:::::" + var);// static variable for static method
}
//This is non static method
void IamNonStaticDemo() {
System.out.println("Non Static Method:::::" + demo +" I can also access:::: " + var); //Since it is a non static method, it can access both string variables
}
} //main class
输出:
Non Static Method:::::Variable I can also access:::: Static Variable
Static Method:::::Static Variable
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.
LinkedIn