import java.util.*;
public Class C
{
final Vector v;
C()
{
v=new Vector();
}
C(int i)
{
//Here, it is an error. v might not have been initialized.
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
public static void main(String[] args)
{
C c=new C();
c.someMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是编译时错误.我知道,但它说(在NetBeans中)变量v应该被初始化.当我在重载的构造函数中初始化它时,它修复了问题并打印"true".我的问题是为什么我应该在重载版本的构造函数中再次初始化它.(我在默认构造函数中初始化了一次)并且我甚至没有使用重载版本.为什么?
在使用JSF来开发使用NetBeans的Web应用程序时,我曾多次注意到在某些情况下,JSF ManagedBeand中的getter方法(以及可能的setter)也会被执行多次,而它们只打算执行一次.在这种情况下,有时执行某些操作(特别是某些计算)的某些关键条件(如果等)以防止它们被淹没是非常关键的.我一直在努力了解其背后的实际原因,但我不能.
在这里,我演示了一个非常简单的应用程序,其中有一个getter方法
public Collection<entity.Country> getCountries(){};
Run Code Online (Sandbox Code Playgroud)
调用远程EJB并从MySql数据库中的相关表中检索所有国家/地区并在JSF页面上显示.网页的屏幕截图如下所示.
没有必要对屏幕截图,JSF页面代码及其相应的ManagedBean付出太多和特别的关注.

这是JSF页面代码
<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Countries</title>
</h:head>
<h:body>
<h:form>
<center><br/><br/><br/>
<h:dataTable id="dataTable" styleClass="table" frame="box" value="#
{country.countries}" var="row" bgcolor="lightyellow" border="7"
cellpadding="7" cellspacing="7" rules="all" width="50%" dir="ltr">
<f:facet id="header" name="header">
<h:outputText value="~:Country:~" styleClass="tableHeader"/>
</f:facet>
<h:column>
<f:facet name="header">Country ID</f:facet>
<h:outputText id="countryID" value="#{row.countryID}"/>
</h:column>
<h:column>
<f:facet name="header">Country Name :</f:facet>
<h:outputText id="countryName" value="#{row.countryName}"/>
</h:column>
</h:dataTable>
</center>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
相应的简单JSF ManagedBean代码就在这里.
package country;
import commonBean.CommomBeanRemote;
import java.util.Collection;
import javax.ejb.EJB; …Run Code Online (Sandbox Code Playgroud) 在Java中是否有任何替代方法可以与foreach{}其他语言(如C#或VB或类似语言)中的其他语言相对应?在某些非常具体的情况下,它有时可能非常有用并且几乎是强制性的.
while(){}Java中的以下循环可以替换为foreach循环或Java中可能存在的与foreach相对应的其他一些概念吗?
public class Demo
{
public static void main(String[] args)
{
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
Iterator<String>it=arrayList.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在Google上进行了长时间的搜索,但我做不到.
在Java中,类永远不能被限定为私有或受保护.它可以随时公开.很明显,毫无疑问.
但是,Java中的内部类可以使用private,protected或public中的任何修饰符指定.内部类也可以在单个块中定义,就像方法一样.在这种情况下,不能使用私有,公共或受保护的任何修饰符指定内部类.
编译以下简单程序没有错误,工作得很好.
package amazingjava;
final class Demo
{
public class Really
{
public class Quite
{
public class Surprising
{
@Override
public String toString()
{
return( "it really works !" );
}
}
}
}
}
final public class Main
{
public static void main(String[] args)
{
System.out.println( "It may be hard to believe, but " + new Demo().new
Really().new Quite().new Surprising().toString());
class Really
{
class Quite
{
class Surprising
{
@Override
public String toString() …Run Code Online (Sandbox Code Playgroud) 让我们看一下下面的代码吧,它完全正常工作.
final class DemoThread
{
public void temp()
{
new Thread(new Runnable()
{
public void run()
{
System.out.println( "Isn't it great ?" ) ;
}
} ) .start() ;
}
}
final public class Main
{
public static void main(String[] args)
{
new DemoThread().temp();
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常并显示消息不是很好吗?在控制台上.这里唯一的问题是为什么Runnable接口不需要由类DemoThread实现?