You would have to create an instance of the class you want to invoke like in the non the static method. It is not really advisable you actually write them on top of each other but for more info see:
You need to first write out the object instance within a given class you want to invoke before you can write the nonstatic variable within the static variable; either from static or non-static.
As long as Salman looks at the examples given in the link I provided and creates an instance like Vsevolod there is no real need to know what exactly he wants to achieve.
yes it is possible and u can try it any time rather than asking here go try it yourself.........its much better to find out yourself than to ask here :P
Yes and no, declaring a variable inside a static method, it automatically becomes Statics ... think like me. Regardless of which class is the method, this method does not belong to the object ... but the class, then there will be only one value for this variable in the application, ie, automatically becomes a static variable.
I think that actually knowing what Salman needs to achieve is necessary to give him the answer he needs, not the answer he wants. They may coincide, but they may not. Just like when users ask for that functionality they absolutely need and describe it in technical terms....
While I agree understanding the intent of a question is often helpful, certainly we can still provide answers to questions that have them,even without knowing the full context. Internet forums (particularly programming forums) are so incredibly littered with Trolls who claim users are asking the wrong question, or coming at the problem wrong. We don't need more of that here.
any variable located in method called local variable it has the same type of outer scope(method ) so it become also static variable in JVM so you do`t need to identify it as a static
In C, and in most other languages that have data structures, we can declare a data structure myStructure, containing one or more elements, and a function myFunction which takes a pointer or reference to myStructure as an argument. So myFunction always operates on an instance of myStructure. Further, we decree that the argument must not ever be null, i.e. we must always have an instance of myStructure before we can call myFunction. So if p is a pointer to myStructure, we would call myFunction(p).
In the foregoing example, the definition of myFunction is tightly bound to that of myStructure. In an OO language such as C++ or Java, this idea is refined and enforced by syntactic rules. Instead of a structure, we can declare a class, let's call it MyClass. Nonstatic data members of a class are similar to the elements of a data structure, in that each instance of a class contains a full copy of the nonstatic data members.
Instead of a function, we can declare a method myMethod, which belongs to the class, so that it can be referred to by its qualified name, MyClass.myMethod in Java or MyClass::myMethod in C++.
Now here's the thing: If myMethod is not declared static, then it implicitly takes an argument which is a reference to an instance of MyClass. The argument doesn't appear in the method's argument list. Inside the method, the argument is always called "this"; and references to data members of MyClass are implicitly qualified by the value of "this" (the context known as "object scope").
In Java, when writing a call to a nonstatic method, you place the reference to MyClass (which becomes "this" inside the method) before the method. So, if r is a reference to an instance of MyClass, then we would call r.myMethod(). This is very much like calling myFunction(p) in the first example, except for all the syntactic niceties that enforce the calling rules and create object scope.
This behavior is just part of the OO principle called "encapsulation", wherein a group of data values is bundled up and associated with a set of behaviors. There is more to the principle than this, but my goal at the moment is to explain not the principle but the mechanism.
class MyClass {
int numberOfToesPerFoot;
int numberOfFeet;
// constructor initializes each instance of MyClass
MyClass(int ntoes, int nfeet)
numberOfToesPerFoot = ntoes;
numberOfFeet = nfeet;
}
void myMethod() {
System.out.println("I have " +
this.numberOfToesPerFoot * this.numberOfFeet +
" toes."); // "this." could be omitted
// with the same result in this context
}
public static void main(String argv[]) {
MyClass r = new MyClass(5, 2); // must create an instance of MyClass
r.myMethod(); // invoke myMethod on instance r
}
Static.
In most respects except for naming, a static data member in Java behaves like a global variable in other languages. There's only one instance of it in the whole environment of the program.
A static method in Java is just a function without any implicit arguments. It doesn't have "this" or "object scope"; you can call it without being in possession of an object of the class to which the method belongs.
When referring to a static method or data member, you can explicitly qualify it by prepending the class name, as in the example below.
class MyClass {
static int numberOfToesPerFoot; // global value for the class MyClass
static int numberOfFeet;
static void initializeStatics(int ntoes, int nfeet) {
I've been asked this sort of question enough times to know that often the "context free" answer is not the right one.
I wasn't implying that the question was right or wrong, I was just trying to understand the context to give an helpful answer. Implicitly accusing me of trolling because of that is rude to say the least. That said, I'll make you happy and avoid posting anything else on this thread.