File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Static Main function VS Static user define function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Main function VS Static user define function" Watch "Static Main function VS Static user define function" New topic
Author

Static Main function VS Static user define function

ravi_be
Greenhorn

Joined: Apr 18, 2001
Posts: 4
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?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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
Raajesh Chandran
Ranch Hand

Joined: May 04, 2001
Posts: 103
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.

chandran..
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you need to access non-static data inside a static method, it needs to be connected to an object:

Dave.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"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.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static Main function VS Static user define function
 
Similar Threads
main() function
static variable inside function definition
Constructor
Anonymous Class
Static initialisation