I have one class which has one static method as shown below.
class A
{
A()
{
Initialize();
}
static void fm()
{
;
}
void Initialize()
{
;
}
}
Now in the program if i call A.fm(), Will it call the Initialize method or not?
Assuming that this is in a language like C++, Java, or C#:
It will not. Constructors only get called when new is used or when a variable of that type (A in this case) is declared as a local variable.
You should be looking for a static constructor, if so and if youre using c# you might wanna run this code. Static constructors grants that you run initializing code before running any other code within the class.
public class A
{
public static void Method()
{
Console.WriteLine("METHOD!!!");
}
public void Method2()
{
Console.WriteLine("INSTANCE METHOD!");
}
static A()
{
Console.WriteLine("STATIC CTOR");
}
}
class Program
{
static void Main(string[] args)
{
A.Method();
new A().Method2();
A.Method();
A.Method();
A.Method();
A.Method();
A.Method();
A.Method();
}
}
Its then the output!
STATIC CTOR
METHOD!!!
INSTANCE METHOD!
METHOD!!!
METHOD!!!
METHOD!!!
METHOD!!!
METHOD!!!
METHOD!!!
In your case, the Initialize will not be called as it is inside a default constructor. If you make your default constructor also static, then the Initialize method will be called first in sequence and after that the fm() method will be called..
Related
I'm trying to use HangFire to call an action method on a class. From the code below, the action method works correctly if called outside of HangFire, but throws an exception when using HangFire. I also tried using Invoke() as stated by other similar posts.
Expression body should be of type
'MethodCallExpression'(Parameter'methodCall')'
I'd like to figure out how to have HangFire execute this type of method if possible.
class Program
{
static void Main(string[] args)
{
var a = new ActionTest();
// Calling this method prints out Hello World correctly
a.DoAction();
GlobalConfiguration.Configuration.UseSqlServerStorage(#"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True");
// Call this method from HangFire produces exception: Expression body should be of type 'MethodCallExpression'(Parameter'methodCall')'
BackgroundJob.Enqueue<ActionTest>(a => a.DoAction());
}
}
public class ActionTest
{
public ActionTest()
{
DoAction = WriteHello;
}
public Action DoAction;
public void WriteHello()
{
Console.WriteLine("Hello World");
}
}
I don't see any other way than wrapping the call to DoAction in a genuine method :
public class ActionTest
{
public ActionTest()
{
DoAction = WriteHello;
}
public Action DoAction;
public void InvokeAction()
{
DoAction();
}
public void WriteHello()
{
Console.WriteLine("Hello World");
}
}
then
BackgroundJob.Enqueue<ActionTest>(a => a.InvokeAction());
This is a question about abstraction.
I want to be able to use two completely different GUIs for my application. They are completely different but implements the same interface.
My question is, what will the constructor look like? What type of object goes in the signature?
They do not extend a common parent, so I can't use polymorphism.
controller object wants to be injected with an object which implements Displayable interface.
interface Displayable {
void display();
}
class Display1 implements Displayable {
public void display() {
//Shows something Fancy on the screen
}
}
class Display2 implements Displayable {
public void display() {
//write something to console
}
}
class Main {
public static void main(String[] args) {
// Controller controller = new Controller(new Display1());
Controller controller = new Controller(new Display2());
controller.display();
}
}
class Controller {
????? display;
public Controller(?????? display) {
this.display = display;
}
public void display() {
display.display();
}
}
I have a JAVA class that has two methods. The first one is the main method and the second one is method1().
Let's say the following is the class:
public class SomeClass() {
public static void main(String[] args) {
SomeClass myObj = new SomeClass();
Map<String,Object> map = new HashMap<String,Object>();
map.put("obj", myObj);
MVEL.eval("System.out.println(\"I am inside main method\");obj.method1();",map);
}
public static void method1(List<String> listOfStrings){
System.out.println("I am inside method 1");
}
}
Now as you can see in the expression, to call method1, I need to pass a list as arguments. How to do that? What changes are required in the expression? What if I want to pass dynamic arguments in my program?
You can create a List or have it coming from some other source as an argument.
Only thing you need to take care is to put inside the map object,
which used by MVEL for evaluation.
Need to pass list as mentioned -> obj.method1(myList);
Working Code Below
public class SomeClass {
public static void main(String[] args) {
SomeClass myObj = new SomeClass();
Map<String, Object> map = new HashMap<String, Object>();
map.put("obj", myObj);
List<String> listOfStrings = new ArrayList<String>();
listOfStrings.add("my ");
listOfStrings.add("List ");
listOfStrings.add("is printing");
map.put("obj", myObj);
map.put("myList", listOfStrings);
MVEL.eval("System.out.println(\"I am inside main method\");obj.method1(myList);",map);
}
public static void method1(List<String> listOfStrings) {
System.out.println("I am inside method 1");
for (String s : listOfStrings) {
System.out.print(s);
}
}
}
output
I am inside main method
I am inside method 1
my List is printing
I wanted to know why this did not work, as in why didn't the compiler invoke the restart method within the computer class...
Consider the following scenario:
I have 3 classes as shown below:
public class Computer {
public int compStatus = 0; //0 means off, 1 means on.
public void turnOn(){
this.compStatus = 1;
}
public void turnOff(){
this.compStatus = 0;
}
public void restart(){
if(compStatus ==1){
System.out.println("Turning off");
compStatus = 0;
System.out.println("Turning on");
compStatus = 1;
System.out.println("Restart successful");
}
}
}
Now the sub-class:
public class Macintosh extends Computer {
public void openXCode(){
if(compStatus == 1){
System.out.println("XCode Compiler opened.");
}
else{
System.out.println("Mac is off.");
}
}
public void restart(){
System.out.println("Mac restarted");
}
}
The tester class:
public class CompTest {
public static void main(String[] args){
Computer testObj = new Macintosh();
testObj.turnOn();
testObj.restart(); ///ERROR HERE
}
}
I am aware that the compiler checks if the restart method is in the class of the reference variable 'Computer' not the class of the actual object at the other end of the reference 'macintosh'. So if what I have said is true, why is the restart method not invoked?
You have to call the base class method in order to actually restart. Your method is just hiding the base method. You should override the method and then call it base.restart to do what you want.
Is there a way to invoke a method after all #After annotated methods of a test method had been run?
I need this for a special framework for my company.
In testng i can use the afterInvocation method, which is called after every configuration method. Is there some alternative in JUnit?
A rule will run after all the #Afters. The ExternalResource could be abused in order to do what you want:
public class VerifyTest {
#Rule public ExternalResource externalResource = new ExternalResource() {
public void after() {
System.out.println("ExternalResource.after");
}
};
#After
public void after1() {
System.out.println("after1");
}
#After
public void after2() {
System.out.println("after2");
}
#Test
public void testVerify throws IOException {
}
}