How to escape Main?

P

Perfect Reign

I'm sure I'm doing something wrong but I cannot find out what. After two
hours of googling and even asking a few co-workers, I give up. What's going
on??

I consistently get this message:

non-static method init() cannot be referenced from a static context

From what I understand, I cannot do something non-static from a static
context. Okay, so I want to write a console app which allows for user
interaction. If I call a method which takes a parameter then I get this
mesage. My latest attept is to put the parameter methods in a non static
public method. But then I can't call this from main either. What's going
on?? If - as in the code below - put the call to init() in main, main
gives me the compile method. If it take it out, then I get nothing. If I
get rid of init and put the code in main, I get a similar compile method.

What do I do??

public class myClass
{
public static void main(String[] args)
{
myClass init = new myClass();
init();
}
public void init() throws Exception
{
//get us out of a static method
String strSearch = "something"
if ( searchFile( strSearch ) == true )
{
//do something here
}
else
{
System.out.println( "No Results Found" );
}
}
private boolean searchFile( String strSearch )
{
//method to search for a string
System.out.println( "You searched for: " + strSearch );
return searchFile;

}
}



--
kai - (e-mail address removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
P

Perfect Reign

Ack! I finally got it! Wierd...

I have to apparently not only declare the class as an object of itself
(huh?) but then have to use the subroutine as a method to the class.

This worked...

public class myClass
{
public static void main(String[] args)
{
myClass startApp = new myClass();
startapp.init();
}
public void init() throws Exception
{
//do stuff here and call variables
}
}


--
kai - (e-mail address removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
S

steepyirl

public static void main(String[] args)
{
myClass init = new myClass();
//init(); Instead, this should be:
init.init();
}
This way, you're calling the non-static method in a *non-static*
context - namely, you're invoking it on a specific instance of myClass.
 
R

Rivky

1) Declare both your serachFile method and your init method as static.
2) declare your String strSearch as static.

The u should be OK.

But I seee someother issues in your code .

1) searchFile can't be both a String and a boolean. In your init method
u declare it as a String byt then return it as a boolan in your
searchFile method.

2) You need to declare your strSearch outside of the init method. For
scoping reasons, if it's declared within your init method, searchFile
will not recognize it.

3) If your init method throws Exception, u need to put a try catch when
ou call init.
 
K

Knute Johnson

Perfect said:
I'm sure I'm doing something wrong but I cannot find out what. After two
hours of googling and even asking a few co-workers, I give up. What's going
on??

I consistently get this message:

non-static method init() cannot be referenced from a static context

From what I understand, I cannot do something non-static from a static
context. Okay, so I want to write a console app which allows for user
interaction. If I call a method which takes a parameter then I get this
mesage. My latest attept is to put the parameter methods in a non static
public method. But then I can't call this from main either. What's going
on?? If - as in the code below - put the call to init() in main, main
gives me the compile method. If it take it out, then I get nothing. If I
get rid of init and put the code in main, I get a similar compile method.

What do I do??

public class myClass
{
public static void main(String[] args)
{
myClass init = new myClass();
init();
}
public void init() throws Exception
{
//get us out of a static method
String strSearch = "something"
if ( searchFile( strSearch ) == true )
{
//do something here
}
else
{
System.out.println( "No Results Found" );
}
}
private boolean searchFile( String strSearch )
{
//method to search for a string
System.out.println( "You searched for: " + strSearch );
return searchFile;

}
}

PR:

You can either make your methods static or instantiate the class and
call the method from the class in the static main.

public class Test {
public static void myMethod() { }
public static void main(String[] args) {
myMethod();
}
}

or

public class Test {
public void myMethod() { }
public static void main(String[] args) {
Test test = new Test();
test.myMethod();
}
}
 
J

Joona I Palaste

Rivky said:
1) Declare both your serachFile method and your init method as static.
2) declare your String strSearch as static.
The u should be OK.

This is a procedural approach. While it works as such, it doesn't take
into advantage Java's object-oriented features. If you want Pascal or C,
you know where to find them.
Anyway it makes the creation of the new object in main() utterly
needless.
But I seee someother issues in your code .
1) searchFile can't be both a String and a boolean. In your init method
u declare it as a String byt then return it as a boolan in your
searchFile method.

This is true.
2) You need to declare your strSearch outside of the init method. For
scoping reasons, if it's declared within your init method, searchFile
will not recognize it.

I don't see any place where he would have declared strSearch inside his
init method. The way I see it, the declarations are all OK.
3) If your init method throws Exception, u need to put a try catch when
ou call init.

This is good style, but not really needed for unchecked exceptions.

And please, write in English, not k3wl d00dsp33k.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
S

Steve Horsley

Perfect said:
I'm sure I'm doing something wrong but I cannot find out what. After two
hours of googling and even asking a few co-workers, I give up. What's going
on??

I consistently get this message:

non-static method init() cannot be referenced from a static context

From what I understand, I cannot do something non-static from a static
context. Okay, so I want to write a console app which allows for user
interaction. If I call a method which takes a parameter then I get this
mesage. My latest attept is to put the parameter methods in a non static
public method. But then I can't call this from main either. What's going
on?? If - as in the code below - put the call to init() in main, main
gives me the compile method. If it take it out, then I get nothing. If I
get rid of init and put the code in main, I get a similar compile method.

What do I do??

You need to understand what static means, and the relationship
between a class and an instance of that class.

A class is a general description if a type of object, like a
blueprint. Imagin the blueprint for a Ford Prefect car. given
that, it is reasonable to imagine methods like getLength(),
getManufacturer(), and so on. These are general aspects that are
not particular to specific instances of Ford Prefect. So these
methods, along with variables such as totalNumberProduced and
recommendedRetailPrice would be "static" or shared things.

In contrast, startEngine(), selectGear('Drive') methods, and
milesTraveled, colour, fuelLevel etc are methods and variables
clearly apply to a specific to an INSTANCE of a Ford Prefect car.

It makes no sense just to call startEngine(). Which one? All of
them everywhere, at once? None of them? This is the equivalent of
your static/non-static context problem.

In your case, init() initialises THIS PARTICULAR MyClass
instance. To be able to do that, you have to make one first, and
then init() THAT PARTICULAR one. Like this:

public static void main(String[] args) {
firstOne = new MyClass();
firstOne.init();
...

You were nearly there too. It takes a while for the difference
between static and non-static to become second nature.

Hope that doesn't feel like I'm talking down to you.

Steve
 
K

knightowl

# public static void main(String[] args)
# {
# myClass init = new myClass();
# init();
# }

I believe that init(), really should be init.init();

HFC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top