Can call a method by class name in same package without create object of class or without inheritance in java
public class BoxWeight /*extends Box*/{
public static void main(String[] args) {
/*BoxWeight myCat = new BoxWeight();*/
/*Box myAnimal = myCat;*/
Box.testClassMethod();<------------ why this possible
/* myAnimal.testInstanceMethod();*/
}
}
public class Box /*extends Bicycle*/{
public static void testClassMethod() {
System.out.println("The class" + " method in Box.");
}
public void testInstanceMethod() {
System.out.println("The instance " + " method in Box.");
}
}
my question is not this as you seems my qestion is this “Can call a method by class name in same package without create object of class or without inheritance in java” but i have fix this i want to confirm is this possible or not
You can call a method with the syntax
ClassName.methodName()if the method is declaredstatic, egMore info about static class members can be found in the Java Tutorials.