Can i call my function which is in the class from the same class with out creating an object and a function which is static | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can i call my function which is in the class from the same class with out creating an object and a function which is static

generally we call a function in one class by creating its object in the main class ...but how can i call a function from the same class with out an object..can anyone quote an example .

9th Aug 2017, 2:54 AM
Satwik Dondapati
Satwik Dondapati - avatar
4 Answers
+ 2
Make the method static. static void myMethod(){ // blahblah... } static void main(// blahblah ){ // call the method myMethod(); }
9th Aug 2017, 3:00 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Some sort of object reference is required to access a non-static member. That's what it means to have something non-static. It only exists with some sort of class instance. You would need to first be in a non-static method in order to call another non-static method directly. Ex/ void aMethod(){ // this is inside an instance myMethod2(); // calling other method from same instance } void myMethod2(){ //blah blah... } You just cannot DIRECTLY call a non-static method FROM a static method. Since the static method is not in an instance of the class, while the non-static method is. Although, if you really wanted to, you could keep track of which instance you are referring to. Ex/ class Ex{ static Ex instance; void aMethod(){ instance = this; } static method(){ instance.method2(); } void method2(){ // stuff.. } }
9th Aug 2017, 3:45 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
i want without static methods
9th Aug 2017, 3:05 AM
Satwik Dondapati
Satwik Dondapati - avatar
0
What you are telling is the process of construction. And if you are using without statics then obj should be make without that I would not think it would be possible.
9th Aug 2017, 4:27 AM
Irwin Lopez
Irwin Lopez - avatar