- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Classes and Objects in BlueJ
Premium Content - Free Preview
This node will cover the basics of using BlueJ and connect it to using Java. You can view a longer BlueJ tutorial on the BlueJ site.
More About BlueJ
BlueJ lets you interact with Objects in your code directly which helps when learning concepts or to try something out quickly. We will see different actions that you can do with BlueJ and also how its done with real code. You can skip the "real code" parts for now, and come back to them to reference later.
Opening a Project
Open up the People Project in BlueJ
- Find the folder where BlueJ is installed and open the examples folder inside.
On Windows, look in C:\Program Files (x86)\BlueJ or C:\Program Files\BlueJ.
If you cannot find it, download a copy of examples and unzip it. - Open the people folder and click on the BlueJ package.
(You can also open a project from within BlueJ by clicking on Project > Open Project. )
BlueJ Class Diagram
You should then see the following screen (without the blue markup):
BlueJ lets you create and view Classes and Objects in a visual manner. The main pane shows a diagram of each Class in a project and their relationships with each other.
Creating Objects
As mentioned before, Classes are like blueprints to define specific instances (or Objects) of code. To create an actual object, right-click on a Class and click on a "new" menu item. For example,
- Right-click on
Staff
to get a menu. - Click on the second menu item,
new Staff()
. - You'll get a box asking you to name your new object, but you can leave the default suggestion and click OK.
Notice how an object now shows up on the object bench.
All you needed when creating that object was its name, but sometimes Objects ask for more information so they can start off with certain data. To see this, let's create one more Object, this time a Student:
- Right-click on the Student Class
- This time, click on
new Student(String, int, String)
- You will now be given a prompt for both an Object name and parameters. You can copy the values shown below, and then click OK. Note there are two String values, which need to be enclosed in quotes.
This time, you were prompted to fill-in the parameters of the Object, so it could set up its initial values.
To create an actual object, you use the new
keyword. This will call up a constructor, a method defined in the Class that "constructs" an instance of the Class. A Class can have different Constructors, each with different parameters. When you create a new object, you need to pass it the parameters specified by its Constructor. The above objects could have been created/instantiated with the following actual code:
staff1 = new Staff();
student1 = new Student("Alice", 1992, "00234");
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
Comments
niveoserenity
Oct 3, 12:54 AMI got Error when attempting task step 2: Cannot find symbol - variable student1
Learneroo
Oct 4, 10:46 AM@niveoserenity, sorry if it wasn't clear, did you create a Student Object called student1 first? (as described above) ?
niveoserenity
Oct 5, 11:10 PMGot it ^
calo
Jan 21, 9:02 AMTask 1 at the end: do I do it by creating a new class for instance: right click -> new class -> and name it database1 or what ? if yes why I dont have there any otpions for adding like addperson, but basic Open editor, compile, remove, inspect? thanks
Learneroo
Jan 21, 9:32 AM@calo, you need to create an instance (an Object) of the class
Database
. So right-click on the icon of the Database class to create an instance of it. (See "Creating Objects" above.)Lukas Dancak
Mar 18, 4:55 AMI cannot create object. But I find answer in BlueJ FAQs:
I cannot create objects or inspect classes
You may right click a class and the only options are "Open Editor", "Compile", "Inspect" (which is greyed out) and "Remove". This problem is easily solved - the class simply needs to be compiled. The best way to do this is to use the "Compile" button which appears at the left hand side of the BlueJ window - this will compile all classes which are presently uncompiled. Alternatively the "Compile" option available in a Class's popup menu can be used to compile that class by itself. Once a class is compiled, the grey diagonal stripes across the class will disappear and the missing options will become available.
David
Mar 18, 12:45 PMWhen i open the package the first message i see is: "Initialising virtual machine...Done. "
At this point i still see the diagonal stripes.
I click on "Compile" and see the message: "Compiling...Done"
The first thing i get in a pop-up is:
"Recompile with -Xliint:unchecked for details"
Followed the steps on this post to disable the pop-up: http://forums.devshed.com/java-help/932889-recompile-xlint-deprecation-details-last-post.html
At this point the diagonal stripes have ONLY disappeared for Staff.
If i try to right click any of the other ones: Database, Person or Student and select "Compile" i get "Compiling...Done" but the diagonal stripes do not disappear.
I'm still doing research, found a couple of posts stating that the methods are old or something like that.
Also noticed that if i create a Staff object and try to compile any of the others which are diagonal stripped it deletes the object.
I checked and have the latest up-to-date as per the Help> Check Version... Crtl+V
-Tried Restarting the JVM on Tools.
-Used Show debugger and reproduced issue but i do not see any logging or trace o__O , should it?
Further comments, suggestions or help are appreciated.
Best Regards,
Thanks.
David
Mar 18, 2:23 PMWell one lunch after finally found out why it was only compiling Staff.
Step 2 from instructions state to go to examples/people and click on the BlueJ package.
people on has defined class for staff
Under the same path examples there is another folder named people2, this one has classes for staff and students.
Wohoooo!
David
Mar 18, 2:24 PM-_- nope didn't work.... scratch last comment.
David
Mar 20, 9:56 AMSo solution for my last 3 comments was... used Windows 8 rather than Windows 7. = )
Jake
Jun 21, 8:09 PMi dont have these examples
Stanley Mwangi
Jun 27, 6:22 PMWhere is the examples folder in linux?
Jake
Jul 3, 9:42 PMwhere are located these examples in linux
Learneroo
Jul 3, 10:37 PM@Jake, I added a download link to the examples folder above.
Tanmay Jain
Jul 19, 7:01 AMWhen I create student it create a student like student() and not like new Student(String name, int yearOfBirth, String studentID)
paul fothergill
Aug 2, 8:35 PMhi guys, right click on the object like "staff" press "compile" then right click it again and you can follow the tutorial. You will get an option for new Staff() etc.
Marc Dandeneau
Apr 5, 9:24 PMThe only thing I am getting when I bring up a new database is "inherited from object" and 2 void methods - help!
thales
Jul 8, 9:36 AMPlease help someone, i cant find the listAll anywere!??
When i compile al previous steps are gone..... And when i dont i cant find listAll either. What to do?
step for step please?
thales
Jul 8, 9:43 AMI cant figure this out!
It does not word for me, i cant find listAll please help. step by step?
Rayyanwilson
Jun 28, 7:42 AMDr. Rayyan Wilson, MDSc, PhD, specializing in Acupuncture and Anatomy in the United States of America.I am committed to promoting wellness through holistic practices, offering comprehensive care
to patients seeking alternative therapies. For better sleep health solutions, use the SweetNight Sleep discount code to access special offers on medically approved mattresses and pillows.
https://www.reecoupons.com/view/sweetnight