Java – Public field in Java

glassfishjavamanaged-bean

I have this Managed bean:

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;   

import org.glassfish.osgicdi.OSGiService;

@Named("ApplicationController")
@SessionScoped

public class Tab1Page implements Serializable {
    public String actionString;

    public Tab1Page() {
    }
    public static final long serialVersionUID = 254415216070877770L;
// Constants
    public final static String hashKey = "tab1PageTab";
    public String actApplicationControllerionString = "";

    @PostConstruct
    public void post() {
        Format formatter;
        Date date = new Date();

        // Time formate 01:12:53 AM
        formatter = new SimpleDateFormat("hh:mm:ss a");
        tabName = formatter.format(date);
        System.out.println("Tab1Page\t" + tabName + "\t@PostConstruct...");
    }

    @PreDestroy
    public void destroy() {
        Format formatter;
        Date date = new Date();

        // Time formate 01:12:53 AM
        formatter = new SimpleDateFormat("hh:mm:ss a");
        tabName = formatter.format(date);
        System.out.println("Tab1Page\t" + tabName + "\t@PreDestroy...");
    }
    String tabName;

    public String getTabName() {
        return this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".")) + "\t" + tabName;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }

    public String getActionString() {
        String actionString = null;
        return actionString;
    }

    public void setActionString(String actionString) {
        this.actionString = actionString;
    }
}

When I try to deploy it on Glassfish I get this error:

[#|2012-04-16T21:42:51.689+0300|INFO|glassfish3.1.2|org.glassfish.osgiweb|_ThreadID=63;_ThreadName=Thread-2;|Removed bundle 416 against context path /HM_57-1.0-SNAPSHOT |#]

[#|2012-04-16T21:42:51.690+0300|WARNING|glassfish3.1.2|org.glassfish.osgijavaeebase|_ThreadID=63;_ThreadName=Thread-2;|Failed to deploy bundle com.DX_57.History-Module-57 [416]
org.glassfish.osgijavaeebase.DeploymentException: Deployment of com.DX_57.History-Module-57 [416] failed because of following reason: Failed while deploying bundle com.DX_57.History-Module-57 [416] : java.lang.RuntimeException: Failed to deploy bundle [ com.DX_57.History-Module-57 [416] ], root cause: Exception while loading the app
    at org.glassfish.osgijavaeebase.AbstractOSGiDeployer.deploy(AbstractOSGiDeployer.java:125)
    at org.glassfish.osgijavaeebase.OSGiContainer.deploy(OSGiContainer.java:154)
    at org.glassfish.osgijavaeebase.JavaEEExtender.deploy(JavaEEExtender.java:107)
    at org.glassfish.osgijavaeebase.JavaEEExtender.access$200(JavaEEExtender.java:61)
    at org.glassfish.osgijavaeebase.JavaEEExtender$HybridBundleTrackerCustomizer$1.call(JavaEEExtender.java:151)
    at org.glassfish.osgijavaeebase.JavaEEExtender$HybridBundleTrackerCustomizer$1.call(JavaEEExtender.java:148)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Failed to deploy bundle [ com.DX_57.History-Module-57 [416] ], root cause: Exception while loading the app
    at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.deploy(OSGiDeploymentRequest.java:196)
    at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.execute(OSGiDeploymentRequest.java:118)
    at org.glassfish.osgijavaeebase.AbstractOSGiDeployer.deploy(AbstractOSGiDeployer.java:121)
    ... 10 more
Caused by: org.glassfish.deployment.common.DeploymentException: WELD-000075 Normal scoped managed bean implementation class has a public field:  public@Named @SessionScoped class com.DX_57.HM_57.Tab1Page
    at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:181)
    at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
    at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:277)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:460)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
    at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.deploy(OSGiDeploymentRequest.java:183)
    ... 12 more
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000075 Normal scoped managed bean implementation class has a public field:  public@Named @SessionScoped class com.DX_57.HM_57.Tab1Page
    at org.jboss.weld.bean.ManagedBean.checkBeanImplementation(ManagedBean.java:438)
    at org.jboss.weld.bean.AbstractClassBean.initialize(AbstractClassBean.java:191)
    at org.jboss.weld.bean.ManagedBean.initialize(ManagedBean.java:322)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer.java:115)
    at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:204)
    at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:344)
    at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:179)
    ... 17 more
|#]

I don't understand what this error means. I modified all methods to be public but still I get this error. How I can correct the code?

Best wishes

Best Answer

The problem is the field (public String actionString;) not the methods.

From docs:

If a managed bean has a public field, it must have scope @Dependent. If a managed bean with a public field declares any scope other than @Dependent, the container automatically detects the problem and treats it as a definition error.