• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Main function VS Static user define function

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With in the static function it is not possible to access to non-static variable.
1.How can the java programme work with a non-static variable inside static main function?
2.What is the difference between static main function with static user define function?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1.How can the java programme work with a non-static variable inside static main function?
your are limited in the use of variable for instace variable only
It is quite normal because when you declare something static this belong to the class and not to one of the instance of a class. So you can use local variable inside your static methode or you can instantiate an object of the required (locally) type inside your static methode and access the instance variable of this class by a getter :more explicitly
public class TestStatic
{
private String myInstanceVariable;
public TestStatic()
{
this.myInstanceVariable = "aString";
System.out.println("constructor instantiate : "+this.myInstanceVariable);
}
public static void main(String[] args)
{
TestStatic.toUpperCase();
}
public static String toUpperCase()
{
TestStatic aTestStaticInstance = new TestStatic();
String upper = aTestStaticInstance.getMyInstanceVariable().toUpperCase();
System.out.println("static methode return : "+upper);
return upper;
}
public String getMyInstanceVariable()
{
return this.myInstanceVariable;
}
}
this kind of thing is often use in the singleton pattern..
Y
2.What is the difference between static main function with static user define function?
What i know is that the main function is the function directly called by the java VM and the parameter are the one set on call to the java VM.
if someone have more information about it.......

------------------
Benjamin l�onard
evisor
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!

We are not calling the main method.The compiler only calls the main method.If U created static method(i.e user defined),in this all the variables are must static.
In Non static method U can call static variables.But U can't call the Non static variables from static method.Hope U understand.If U can't understand I will tell U much better way
chandran.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to access non-static data inside a static method, it needs to be connected to an object:

Dave.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"ravi_be",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic