Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • copeg

    by Published on November 1st, 2011 02:23 PM
    1. Categories:
    2. Java Tutorials

    Oftentimes constructing a class might require many different variables - in short a particular class might need to be built with a range of options; options that are only decided at runtime. Creating a constructor that accepts several different parameters is an option, as is using a series of setter methods as needed (see below).

    As an example, lets build a Pizza. The following class contains the information we want to specify for our pizza - it allows us to specify the toppings, if we want it delivered, etc...This is a simple class, all of its variables are private and accessed only through the modifier get/set methods. For brevity I have left out comments in the code.

    import java.util.ArrayList;
    import java.util.List;
     
    public class Pizza{
     
    	private String crustType = "Thick";
    	private boolean extraCheese = false;
    	private boolean forDelivery = false;
    	private List<String> toppings = new ArrayList<String>();
     
    	public Pizza(){
     
    	}
     
    	public Pizza(String crust, boolean extraCheese, boolean forDelivery, String...toppings){
    		this.crustType = crust;
    		this.extraCheese = extraCheese;
    		this.forDelivery = forDelivery;
    		for ( String t : toppings ){
    			this.topings.add(t);
    		}
    	}
     
    	public String getCrustType() {
    		return crustType;
    	}
    	public void setCrustType(String crustType) {
    		this.crustType = crustType;
    	}
    	public boolean isExtraCheese() {
    		return extraCheese;
    	}
    	public void setExtraCheese(boolean extraCheese) {
    		this.extraCheese = extraCheese;
    	}
    	public boolean isForDelivery() {
    		return forDelivery;
    	}
    	public void setForDelivery(boolean forDelivery) {
    		this.forDelivery = forDelivery;
    	}
    	public List<String> getToppings() {
    		return toppings;
    	}
    	public void addTopping(String topping) {
    		this.toppings.add(topping);
    	}
    }
    ...
    by Published on October 10th, 2011 04:50 PM
    1. Categories:
    2. Official Forum Rules

    How you phrase a question goes hand in hand with the answers you will receive. A well formed question often receives a well formed answer. A poorly formed question a general answer, if any at all. Below are some tips to help one form a question in a manner that increases the chances of receiving help:

    Ask a specific question. General out of context question - such as 'what am I doing wrong' or 'I'm stuck, what do I do next', or just posting a homework assignment - makes it difficult for contributors to point you in the right direction. Rather, rephrasing these questions - 'I get the following compile time error in the following code, what is causing it and why?' followed by the code and error - results in much more context and makes it much easier for contributors to answer. When posting questions specific about code, you should ask and answer the following question (and information such as error messages and stack traces to the question itself).
    • Does it compile?
    • Are there exceptions?
    • Does it function as expected?
    ...
    by Published on March 5th, 2011 02:02 PM
    1. Categories:
    2. Common Java Mistakes

    Problem description: JDBC Connection Error
    Problem category: Runtime Error

    Diagnosis Difficulty: Easy to medium
    Difficulty to Fix: Easy


    When trying to connect to a database using JDBC, a java.lang.ClassNotFound exception is thrown at the point where the database driver should be loaded (eg when Class.forName is called for the appropriate database driver).

    Suggested fixes

    This typically occurs when the database driver is not on the classpath. There are two steps to fix this problem. First, download the driver for the database you wish to access (mysql, postgres, Oracle, Microsoft SQL). Second, place the downloaded jar file on the classpath of the program. ...
    by Published on March 5th, 2011 02:01 PM
    1. Categories:
    2. Common Java Mistakes

    OutOfMemoryError: Java heap space
    Problem category: Runtime Error

    Diagnosis Difficulty: Medium-Hard
    Difficulty to Fix: Easy


    The JVM's default memory allocation is typically enough for a simple program. However, larger programs with memory intensive computation may result in throwing a java.lang.OutOfMemoryError exception during the execution of a program. A more fundamental analysis of memory usage and object allocation should be performed to confirm that the problem isn't an issue with the program design (such as keeping references to non-needed object), otherwise setting the maximum heap size may just cover up a more fundamental problem.

    Suggested fixes

    Increase the java virtual machine's default memory using the -Xmx option. By default, the maximum memory usage for the JVM is 64M. To increase this value, upon execution of the program pass the JVM the -Xmx* option, where * equals the requested memory (typically divisible by 64 until it reaches the gigabyte stage). For example, to run the program from the command line, call:
    ...
    by Published on March 5th, 2011 01:17 PM
    1. Categories:
    2. Official Forum Rules

    I've written shorter versions of this many times, and rather than write it several times again in the future I thought I would centralize its location and just provide a link here. This article is for everyone who hasn't heard of the issues to do with cross posting.

    To put it bluntly, cross-posting the same question to multiple forums can be considered poor forum etiquette. While some forums have strict rules against cross-posting, others are quite lenient about it, and the rules for each forum should be abided by should you wish your question to be answer politely and efficiently (JPF Crossposting Rules). Why is cross-posting considered poor etiquette? ...