Need help with usage of static functions
Hi,
I tried instantiating objects within static functions as follows: It gives error:
If someone can explain an alternate way of doing things, I would appreciate:
public class C {
/**
* @param args
*/
public static void main(String[] args) {
Y.Fn1();
}
}
public class X {
public void Fn2(){
int b=2;
System.out.println("hello");
}
}
public class Y {
public static void Fn1()
{
int a=2;
X obj = new X();
obj.Fn2();
}
}
The actual error is:
Cannot make a static referencxe to the nonstatic method Fn2() from the type X Y.java
Need Help : If someone can comment on the following, I would appreciate:
I have a class Session, which has a method ProcessEvent()
public class Session{
public synchronized void processEvent(Event e)
{
// Processing of the event
// Generate a response for the Event
}
}
This Session class is the mainstream code..
Now I want to write a TestClass that will test the main code.
So I have a Test class as below:
public class Test {
public static void sendReq()
{
// Construct an event and call processEvent Function from the main code
Event ev = new Event();
// Fill the event with parameters
..
Session sess = new Session();
sess.processEvent(ev);
}
}
What I am thinking of doing is: Call the static method in another class called Manager of the mainstream code for test purpose as follows:
public class Manager {
public void somefunction()
{
//*** Just for test purpose: do the following line **//
Test.sendReq();
}
}
I am not sure what I am trying to do will work. I would appreciate if you can comment on that or suggest something that would work for me.
Another thing which I want to do is:
Inside ProcessEvent(), I want to call another static method ParseandValidateResponse() of the TestClass This looks odd, but as long as it works, it would be fine for me because this is only for test purpose.
In this case, I would write,
public class Session{
public synchronized void processEvent(Event e)
{
// Processing of the event
// Generate a response for the Event
Test.parseandValidateResponse(Response res);
}
public class Test {
public static void sendReq()
{
// AS SHOWN PREVIOUSLY
}
public static void parseandValidateResponse(Response resp)
{
// validate the response
}
}

