How to Solve Error Message A managed bean with public field (“connection”) should not declares any scope than @Dependent when Creating Java Bean

Posted on

Introduction

Another article discussing about a specific error message exist as the content of it. Actually, when there is a declaration for defining a variable with the name of ‘connection’ in a Java bean file, there is an error message appeaer. The error message appear in the NetBeans IDE where it is in the following line :

A managed bean with public field ("connection") should not declares any scope than @Dependent

The following is the image which describing the error message in the NetBeans IDE :

How to Solve Error Message A managed bean with public field ("connection") should not declares any scope than @Dependent when Creating Java Bean
How to Solve Error Message A managed bean with public field (“connection”) should not declares any scope than @Dependent when Creating Java Bean

The first declaration of the connection variable with the type of ‘Connection’ is using the public access reserved keyword.

Solution for Solving the Problem

Actually, before getting on into the solution, there is a hint in the NetBeans IDE for a solution recommendation. The hint is available in the above part. Actually, since this bean has a specific scope which is ‘@ViewScoped’, there must not be any variable, property or attribute with ‘public’ access reserved keyword. In order to solve it, just change the access reserved keyword of the connection variable into another one. For an example, in this context, change it into private access reserved keyword. So, the Java bean file revision will be exist as follows :

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.web.view;

import com.mycompany.web.db.DBConnection;
import java.io.Serializable;
import java.sql.Connection;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

/**
*
* @author Mark Spectre
*/
@Named(value = "dbConnectionTestView")
@ViewScoped
public class DBConnectionTestView implements Serializable {
     private Connection connection;
     String DEFAULT_DB_CONNECTION = "Not Connected";
     private String status = DEFAULT_DB_CONNECTION;
     String DB_CONNECT_SUCCESS = "Database Connection is a success";
     String DB_CONNECT_FAILED = "Database Connection is failed";
     static void init(){
           String url = "jdbc:postgresql://localhost:5432/employee";
           String username = "postgres";
           String password = "password";
     }
}

Leave a Reply