Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static and non static variables

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can static method access non-static variables from subclass/any other class?
Can non-static method access static variables/final static variables from subclass/any other class?

Please expalin with example.


 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can static method access non-static variables from subclass/any other class?
Can non-static method access static variables/final static variables from subclass/any other class?


Static methods can't access non-static variable because you can acccess static method through class name so how do you know which objects non-static instance variable you are accessing

yes non tatic method can access static variables
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dinesh tahiliani:


Please expalin with example.



Why not just try yourself with an example an see what happens? That way you will remember the details for a longer time (if not forever).
After you have tried, if you have any issue with understanding the way it works, come back and ask.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello There,


Can static method access non-static variables from subclass/any other class?



Yes they may:-




Can non-static method access static variables/final static variables from subclass/any other class?



Yes they may,
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simpler example. Hope this explains...

public class StaticTest {

static String s1 = "Static string";
String s2 = "Normal String";

void nonStaticMethod() {
System.out.println("In nonStaticMethod(): " + s1);
System.out.println("In nonStaticMethod(): " + s2);
}

static void staticMethod() {
System.out.println("In staticMethod(): " + s1);
//System.out.println(s2); //ERROR, due to static
}

public static void main(String[] args) {
System.out.println("In main(): " + s1);
//System.out.println(s2); //ERROR, Can't make static reference to non-static field
//nonStaticMethod(); //ERROR, Can't make static reference to non-static method
staticMethod();
}
}
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But from what Raef said, you could reference the static variable by using class.static variable, right? So the above example can be modified as follows:

public class StaticTest {

static String s1 = "Static string";
String s2 = "Normal String";

void nonStaticMethod() {
System.out.println("In nonStaticMethod(): " + s1);
System.out.println("In nonStaticMethod(): " + s2);
}

static void staticMethod() {
System.out.println("In staticMethod(): " + s1);
System.out.println(new StaticTest().s2); //can use now with class.staticVariable
}

public static void main(String[] args) {
System.out.println("In main(): " + s1);
System.out.println(new StaticTest().s2); //can use now with class.staticVariable
new StaticTest().nonStaticMethod(); //can use now with class.staticMethod()
staticMethod();
}
}

So would that be correct to say that you cannot use non-static variable BY ITSELF in a static method?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucy,
public class StaticTest {

static String s1 = "Static string";
String s2 = "Normal String";

void nonStaticMethod() {
System.out.println("In nonStaticMethod(): " + s1);
System.out.println("In nonStaticMethod(): " + s2);
}

static void staticMethod() {
System.out.println("In staticMethod(): " + s1);
System.out.println(new StaticTest().s2); //can use now with class.staticVariable
}

In the above code, you have s2 as normal variable(non-static variable), then how can you access this with class name.Non static variables are accessed thru object reference. Can you little elaborate on this plss

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dinesh tahiliani:
In the above code, you have s2 as normal variable(non-static variable), then how can you access this with class name.

Since s2 is not accessed by class name. Access by class name means StaticTest.s2, and this is only available for static members. Instead, Lucy has created a new instance of the class, and then s2 is accessible through that instance.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lucky Lucy:

So would that be correct to say that you cannot use non-static variable BY ITSELF in a static method?



Yes. Thats correct. If at all you have to use, you have to have an instance as the instances can access the static variables.

Welcome to JavaRanch, Lucky Lucy!
[ August 10, 2007: Message edited by: Raghavan Muthu ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

Originally posted by Lucky Lucy:
So would that be correct to say that you cannot use non-static variable BY ITSELF in a static method?



Yes you could say so. Because a non-static variable never stands for itself, it belongs to an object. Contrary to this a static variable belongs to the class.
If you want to access a non-static method of the same class, you will need an object of the class.
That's what you did in your line
new StaticTest().nonStaticMethod(); //can use now with class.staticMethod()

non-static fields (variables and methods) belong to the object, and every object has its own one.
static fields belong to the class, and it doesn't matter how many objects there are, when a variable is static, there is only one for the class.

All objects share this static variables.
This can be a trap. Normally you access static variables through its class name (or without any name when it is in the same class).



But it is also allowed (but should be avoided) to access them via an objects reference.

Example:


The output is:Harry's legs: 6
Frank's legs: 6
Incremented Harry's legs.
Harry's legs: 7
What happened to Frank?
Frank's legs: 7


If you make the variable non-static, everything would be all right. Because the legs variable should belong to the object and not to the class.





Now something els:
Dear "Lucky Lucy" !

Thanks for your first contribution to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:

Now something els:
Dear "Lucky Lucy" !

Thanks for your first contribution to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.




That was a nice example Bu. Is the name "Lucky Lucy" look like a ficticious one? I dint get that that why i dint suggest her to do so!
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's talk Java
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raef Kandeel:
let's talk Java



What else is being talked about here?
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic