The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
May 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Create an instance of the class inside main( )

Posted by Brent Stains on January 17, 2001 at 2:07 PM

> I have a question reg. public.static.void.main().

> main() method should be declared as static as we are accessing the
> method before instantiating.

> Then static methods can access only static methods and static
> variables as per Java rules.

> How is it possible for the public.static.void.main()
> to access non-static methods and variables ???
> I am a beginner and any help in this regard would be thankful.

> Regards,
> Vamsi Sudha.


For example:

public class Test {

private String tempString;

public Test(String inString) {
setTempString(inString);
}

public static void main (String[] args) {
Test t = new Test("Hello");
System.out.println(t.getTempString());
}

public String getTempString() {
return this.tempString;
}

public void setTempString(String istr) {
this.tempString = istr;
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us