+ 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 .
4 odpowiedzi
+ 2
Make the method static.
static void myMethod(){
// blahblah...
}
static void main(// blahblah
){
// call the method
myMethod();
}
+ 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..
}
}
+ 1
i want without static methods
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.