Reputation: 741
public class example {
int a = 0;
public void m() {
int b = this.a;
}
public static void main(String[] args) {
int c = this.a;
}
}
I am new in java. Why I cannot use "this" in the main method?
Upvotes: 4
Views: 5090
Reputation: 41935
public class Example {
int a = 0;
// This is a non-static method, so it is attached with an instance
// of class Example and allows use of "this".
public void m() {
int b = this.a;
}
// This is a static method, so it is attached with the class - Example
// (not instance) and so it does not allow use of "this".
public static void main(String[] args) {
int c = this.a; // Cannot use "this" in a static context.
}
}
Upvotes: 1
Reputation: 3723
Additional of Andreas Fester answer:
/****GRAMMER:
*
* Why cannot use "this" in the main method?
*
* -----------------------------------------------------------------------------------
*
* "this" refers to the current object. However, the main method is static,
* which means that it is attached to the class, not to an object instance.
*
* Which means that the static method can run without instantiate an object,
* Hence, there is no current "this" object inside of main().
*
*
*/
public class Test{
int a ;
public static void main(String[] args) {
int c = this.a; // Cannot use "this" in a static context,
// because there isn't a "this".
}
}
Upvotes: 0
Reputation: 36630
this
refers to the current object. However, the main
method is static, which means that it is attached to the class, not to an object instance, hence there is no current object inside main()
.
In order to use this
, you need to create an instance of your class (actually, in this example, you do not use this
since you have a separate object reference. But you could use this
inside your m()
method, for example, because m()
is an instance method which lives in the context of an object):
public static void main(String[] args){
example e = new example();
int c=e.a;
}
By the way: You should get familiar with the Java naming conventions - Class names usually start with a capital letter.
Upvotes: 14
Reputation: 7676
The main
method is static, meaning it can be called without instantiating example
class (static methods can also be referred to as Class methods). This means that in the context of main
, the variable a
may not exist (because example
is not an instantiated object). Likewise, you can't call m
from main
without first instantiating example
:
public class example {
int a=0;
public void m(){
// Do stuff
}
public static void main(String[] args){
example x = new example();
x.m(); // This works
m(); // This doesn't work
}
}
Upvotes: 0
Reputation: 4088
In addition to comments from Andreas
If you would like to use 'a'. Then you will have to instantiate new example [better make example class name as capital Eaxmple].
Something like
public static void main(String[] args) {
Example e = new Example();
int c = e.a;
}
HTH
Upvotes: 0
Reputation: 2472
Becasue main is static
. To access a
you should declare it as static
as well. Static variables exist without any instance of the class. Non-static variables exist only if an instance exist, and each instance has its own attribute named a
.
Upvotes: 1
Reputation: 1849
You must create an instance of example
example e = new example()
e.m()
Upvotes: 2