Accessing non-static members through the main method in Java
up vote
8
down vote
favorite
As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!
java
add a comment |
up vote
8
down vote
favorite
As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!
java
How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!
java
As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!
java
java
asked Oct 25 '11 at 18:04
Bhavesh
88228
88228
How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34
add a comment |
How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34
How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34
How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34
add a comment |
7 Answers
7
active
oldest
votes
up vote
20
down vote
accepted
The main method does not have access to non-static members either.
public class Snippet
{
private String instanceVariable;
private static String staticVariable;
public String instanceMethod()
{
return "instance";
}
public static String staticMethod()
{
return "static";
}
public static void main(String args)
{
System.out.println(staticVariable); // ok
System.out.println(Snippet.staticMethod()); // ok
System.out.println(new Snippet().instanceMethod()); // ok
System.out.println(new Snippet().instanceVariable); // ok
System.out.println(Snippet.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}
add a comment |
up vote
5
down vote
By creating an object of that class.
public class Test {
int x;
public static void main(String args) {
Test t = new Test();
t.x = 5;
}
}
add a comment |
up vote
2
down vote
YourClass inst = new YourClass();
inst.nonStaticMethod();
inst.nonStaticField = 5;
add a comment |
up vote
2
down vote
The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.
This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.
In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.
To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.
add a comment |
up vote
1
down vote
Through the reference to the object.
public class A {
private String field;
public static void main(String... args) {
//System.out.println(field); <-- can not do this
A a = new A();
System.out.println(a.field); //<-- access instance var field that belongs to the instance a
}
}
add a comment |
up vote
1
down vote
You have to instantiate the object you wish to access.
For example
public class MyClass{
private String myData = "data";
public String getData(){
return myData;
}
public static void main(String args){
MyClass obj = new MyClass();
System.out.println(obj.getData());
}
}
add a comment |
up vote
0
down vote
You must create an instance of the class in order to reference instance variables & methods.
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
20
down vote
accepted
The main method does not have access to non-static members either.
public class Snippet
{
private String instanceVariable;
private static String staticVariable;
public String instanceMethod()
{
return "instance";
}
public static String staticMethod()
{
return "static";
}
public static void main(String args)
{
System.out.println(staticVariable); // ok
System.out.println(Snippet.staticMethod()); // ok
System.out.println(new Snippet().instanceMethod()); // ok
System.out.println(new Snippet().instanceVariable); // ok
System.out.println(Snippet.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}
add a comment |
up vote
20
down vote
accepted
The main method does not have access to non-static members either.
public class Snippet
{
private String instanceVariable;
private static String staticVariable;
public String instanceMethod()
{
return "instance";
}
public static String staticMethod()
{
return "static";
}
public static void main(String args)
{
System.out.println(staticVariable); // ok
System.out.println(Snippet.staticMethod()); // ok
System.out.println(new Snippet().instanceMethod()); // ok
System.out.println(new Snippet().instanceVariable); // ok
System.out.println(Snippet.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}
add a comment |
up vote
20
down vote
accepted
up vote
20
down vote
accepted
The main method does not have access to non-static members either.
public class Snippet
{
private String instanceVariable;
private static String staticVariable;
public String instanceMethod()
{
return "instance";
}
public static String staticMethod()
{
return "static";
}
public static void main(String args)
{
System.out.println(staticVariable); // ok
System.out.println(Snippet.staticMethod()); // ok
System.out.println(new Snippet().instanceMethod()); // ok
System.out.println(new Snippet().instanceVariable); // ok
System.out.println(Snippet.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}
The main method does not have access to non-static members either.
public class Snippet
{
private String instanceVariable;
private static String staticVariable;
public String instanceMethod()
{
return "instance";
}
public static String staticMethod()
{
return "static";
}
public static void main(String args)
{
System.out.println(staticVariable); // ok
System.out.println(Snippet.staticMethod()); // ok
System.out.println(new Snippet().instanceMethod()); // ok
System.out.println(new Snippet().instanceVariable); // ok
System.out.println(Snippet.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}
answered Oct 25 '11 at 18:07
tjg184
3,44111644
3,44111644
add a comment |
add a comment |
up vote
5
down vote
By creating an object of that class.
public class Test {
int x;
public static void main(String args) {
Test t = new Test();
t.x = 5;
}
}
add a comment |
up vote
5
down vote
By creating an object of that class.
public class Test {
int x;
public static void main(String args) {
Test t = new Test();
t.x = 5;
}
}
add a comment |
up vote
5
down vote
up vote
5
down vote
By creating an object of that class.
public class Test {
int x;
public static void main(String args) {
Test t = new Test();
t.x = 5;
}
}
By creating an object of that class.
public class Test {
int x;
public static void main(String args) {
Test t = new Test();
t.x = 5;
}
}
answered Oct 25 '11 at 18:06
Oliver Charlesworth
225k25465593
225k25465593
add a comment |
add a comment |
up vote
2
down vote
YourClass inst = new YourClass();
inst.nonStaticMethod();
inst.nonStaticField = 5;
add a comment |
up vote
2
down vote
YourClass inst = new YourClass();
inst.nonStaticMethod();
inst.nonStaticField = 5;
add a comment |
up vote
2
down vote
up vote
2
down vote
YourClass inst = new YourClass();
inst.nonStaticMethod();
inst.nonStaticField = 5;
YourClass inst = new YourClass();
inst.nonStaticMethod();
inst.nonStaticField = 5;
answered Oct 25 '11 at 18:07
xthexder
1,421818
1,421818
add a comment |
add a comment |
up vote
2
down vote
The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.
This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.
In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.
To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.
add a comment |
up vote
2
down vote
The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.
This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.
In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.
To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.
add a comment |
up vote
2
down vote
up vote
2
down vote
The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.
This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.
In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.
To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.
The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.
This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.
In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.
To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.
answered Oct 25 '11 at 18:12
Desmond Zhou
1,1681017
1,1681017
add a comment |
add a comment |
up vote
1
down vote
Through the reference to the object.
public class A {
private String field;
public static void main(String... args) {
//System.out.println(field); <-- can not do this
A a = new A();
System.out.println(a.field); //<-- access instance var field that belongs to the instance a
}
}
add a comment |
up vote
1
down vote
Through the reference to the object.
public class A {
private String field;
public static void main(String... args) {
//System.out.println(field); <-- can not do this
A a = new A();
System.out.println(a.field); //<-- access instance var field that belongs to the instance a
}
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Through the reference to the object.
public class A {
private String field;
public static void main(String... args) {
//System.out.println(field); <-- can not do this
A a = new A();
System.out.println(a.field); //<-- access instance var field that belongs to the instance a
}
}
Through the reference to the object.
public class A {
private String field;
public static void main(String... args) {
//System.out.println(field); <-- can not do this
A a = new A();
System.out.println(a.field); //<-- access instance var field that belongs to the instance a
}
}
answered Oct 25 '11 at 18:07
Bhesh Gurung
40.8k2074128
40.8k2074128
add a comment |
add a comment |
up vote
1
down vote
You have to instantiate the object you wish to access.
For example
public class MyClass{
private String myData = "data";
public String getData(){
return myData;
}
public static void main(String args){
MyClass obj = new MyClass();
System.out.println(obj.getData());
}
}
add a comment |
up vote
1
down vote
You have to instantiate the object you wish to access.
For example
public class MyClass{
private String myData = "data";
public String getData(){
return myData;
}
public static void main(String args){
MyClass obj = new MyClass();
System.out.println(obj.getData());
}
}
add a comment |
up vote
1
down vote
up vote
1
down vote
You have to instantiate the object you wish to access.
For example
public class MyClass{
private String myData = "data";
public String getData(){
return myData;
}
public static void main(String args){
MyClass obj = new MyClass();
System.out.println(obj.getData());
}
}
You have to instantiate the object you wish to access.
For example
public class MyClass{
private String myData = "data";
public String getData(){
return myData;
}
public static void main(String args){
MyClass obj = new MyClass();
System.out.println(obj.getData());
}
}
answered Oct 25 '11 at 18:09
Chris Thompson
28.7k96896
28.7k96896
add a comment |
add a comment |
up vote
0
down vote
You must create an instance of the class in order to reference instance variables & methods.
add a comment |
up vote
0
down vote
You must create an instance of the class in order to reference instance variables & methods.
add a comment |
up vote
0
down vote
up vote
0
down vote
You must create an instance of the class in order to reference instance variables & methods.
You must create an instance of the class in order to reference instance variables & methods.
edited Aug 20 at 17:36
Rudra
408
408
answered Oct 25 '11 at 18:07
dbreaux
4,12911449
4,12911449
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7893755%2faccessing-non-static-members-through-the-main-method-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34