#declare 2 time a variable

22 messages · Page 1 of 1 (latest)

brittle fractal
#
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package shopping;

import java.util.ArrayList;

/**
 * A simple ShoppingBag
 * 
 * Stores items in an ArrayList
 * 
 * Possible to add  items
 * Also can "empty" the bag
 * 
 * You should NOT need to amend this code at all!
 * 
 * NOTE: 'Object' is the main base class for everything in Java. This means that everything 'is an' Object
 * ANy class you create will work with the shipping bag - you don't need to change this code!
 * 
 * @author Ken
 */
public class ShoppingBag {
    
    private ArrayList<Object> items;
    
    /**
     * Create a shopping bag by initialising the items to an empty arraylist
     */
    public ShoppingBag(){
        
        items = new ArrayList<Object>();
    }
    
    /**
     * Add an item to the shopping bag
     * @param someItem the item that has been bought
     */
    public void buyItem(Object someItem){
        
        this.items.add(someItem);
    }
    
 
    /**
     * create a formatted String containing all the items in the ShoppingBag
     * @return the formatted String
     * Also clears the items list -- 'empties the bag'
     */
    public String emptyBag(){
        
        String itemList = "";
        
        for(Object item: this.items){
            
            itemList = itemList + item.toString() + "\n";
        }
        
        this.items.clear();
        
        return itemList;
    }
}

guys can someone tell me why it is declare "items" to time? one is private ArrayList<Object> items; another is items = new ArrayList<Object>();. first one is just saying data type and second one run to constructer ? if it is we already also declare data type when we run the constructer, why we need to first one ?

tough totemBOT
#

This post has been reserved for your question.

Hey @brittle fractal! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hushed drum
#

only the first one is a declaration

#

it only gives the type to the variable

#

the second is an assignment, and an initialization in this case. it only gives the value to the variable

brittle fractal
hushed drum
#

you can't, it only looks like you can

#

/run

import java.util.ArrayList;
ArrayList<Object> items;
System.out.println(items.size());
cedar pathBOT
#

@hushed drum I only received java(15.0.2) error output

file0.code.java:4: error: variable items might not have been initialized
System.out.println(items.size());
                   ^
1 error
error: compilation failed
hushed drum
#

(it's different here because these are local variables, but you get the idea)

#

the second one initializes the variable so it's actually usable

brittle fractal
#

but I am doing that processes at my other coding. for example :

hushed drum
#

you aren't

hushed drum
#

that's the difference

#

you need both a type and a value to get a complete, usable variable
a declaration, Type name;, only gives the type
an assignment, name = value;, only gives the value
a definition, Type name = value;, gives both

hushed drum
hushed drum
brittle fractal
#

ok I got it.

#

thank you so much man 😗

tough totemBOT
# brittle fractal thank you so much man 😗

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.