0

Java/Spring newbie here. I've a question about auto wiring through SpringBoot.

I have an interface , an implementation , a main class and a configuration class like so :

ServiceInterface.java

public interface ServiceInterface {
    static String serviceName = "service";
    public void displayMessage();
    public String getServiceName(); 
}

ServiceImpl1.java

public class ServiceImpl1 implements ServiceInterface{
    static String serviceName = "default service value ";

    public String getServiceName() {
        return serviceName;
    }

@Override
public void displayMessage()
    {
        System.out.println("This is implementation 1");
    }
}

The main class :

@SpringBootApplication
public class App implements CommandLineRunner{

@Autowired
private ServiceInterface serviceInterface;

public static void main(String args[])
{
    SpringApplication.run(App.class, args);

}

@Override
public void run(String... args) {
    serviceInterface.displayMessage();
    System.out.println(serviceInterface.getServiceName());
    System.out.println(serviceInterface.serviceName );
}
}

AppConfig.java

@Configuration
public class AppConfig {

@Bean
ServiceInterface serviceInterface()
{
    return new ServiceImpl1();
}
}

When I run the code , I get this as the output

This is implementation 1
service 1 
default service value

Why is the variable 'serviceName' inside ServiceImpl1 implementation not accessible through the object created by Spring through autowiring ?

anset
  • 201
  • 2
  • 3
  • 8

1 Answers1

0

Never mind.. I just figured out that variables declared inside an interface are static and final.

Community
  • 1
  • 1
anset
  • 201
  • 2
  • 3
  • 8