JAVA Packages

what are packages?

Page 1 of 1

13 Replies - 1168 Views - Last Post: 20 January 2010 - 12:12 PM Rate Topic: -----

#1 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

JAVA Packages

Post icon  Posted 18 January 2010 - 05:46 AM

hi Everyone,

i am new to java programming,i wanted to know that does java package creates a new folder in windows.....i am actually not getting the exact meaning or you can say exact use of packages in java.For example if i write this package.software.java.programs...now will this create a new folder....plz help as soon as possible...i am pretty confused.


Thanx
Vyom
Is This A Good Question/Topic? 0
  • +

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: JAVA Packages

Posted 18 January 2010 - 06:25 AM

Basically its just as the definition of Package describes, a collection of things. In a programming sense its a way of keeping similar libraries of classes together. Each time you build a jar file you are building a package of sorts with your class files within it.

Packages follow the directory structure idea, meaning a package such as
java.util.Scanner will be organised into a source folder structure.
For example in netbeans i have created a simple application with some packages and classes, i will show you the way the packages look and also the way they are organised in my projects directory.

Below is an example of it, firstly is the class / package structure and secondly is the way files are stored on using the file system based on their package

dreamincode.forums.cpphelpforums.*;
dreamincode.forums.javahelpforums.*;
dreamincode.snippets.cppcodesnippets.*;
dreamincode.snippets.javacodesnippets.*;

File Structure

Attached Image

Hope this cleared it up a bit

This post has been edited by bbq: 18 January 2010 - 06:32 AM

Was This Post Helpful? 1
  • +
  • -

#4 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: JAVA Packages

Posted 18 January 2010 - 10:11 PM

that means to create a package....we have to first create a folder in windows and then compile it using javac by coming out of that folder???
Was This Post Helpful? 0
  • +
  • -

#6 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: JAVA Packages

Posted 18 January 2010 - 11:02 PM

Many IDE's will automatically create the folders, but if you are compiling from command line then yes, you will need to create your own package structure. For small programs it is alright to have single level package hierarchies though, no something.something required, just something is fine.

So then your .java file would be in the folder something and would contain the following line as the first line in the file:

package something;

Hope that clears up everything on packages for you! :)
Was This Post Helpful? 1
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • Joined: 20-September 08

Re: JAVA Packages

Posted 19 January 2010 - 03:12 AM

Quote

that means to create a package....we have to first create a folder in windows and then compile it using javac by coming out of that folder???


Given the source code:

package a.b.c;
class X{}



Compiling thus at the comand line will create the package directory structure for you

javac -d . X.java

This post has been edited by g00se: 19 January 2010 - 03:13 AM

Was This Post Helpful? 1
  • +
  • -

#8 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: JAVA Packages

Posted 19 January 2010 - 10:20 AM

thank you very much for your help....i got that and iam starting to love this forum :D ....one more question:-

we create a object like this in java
box t1=new box();
here we are using a box() constructor.....so y do we need to define another constructor.....when we can create object with the above line and what is the use of this keyword in java???


thanx
Vyom
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: JAVA Packages

Posted 19 January 2010 - 10:27 AM

That line above is calling the constructor. If you haven't built a constructor, that line will likely do nothing. If you do not make your own no-argument constructor (like the one you called in that statement), then the compilor will basically "make" one for you, but it will do nothing. In order for your class to do anything right off the bat, you need to make one to accept parameters or do calculations. If you just want to create classes so you can use it's methods without doing any initial calculations/assignments, then you can just leave it empty. It's good form to make one though, even if it is empty.
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: JAVA Packages

Posted 19 January 2010 - 06:50 PM

Put that one into your Favorites... it is all what you need

http://java.sun.com/...asicsindex.html
Was This Post Helpful? 0
  • +
  • -

#11 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: JAVA Packages

Posted 19 January 2010 - 11:22 PM

View Postvyom.dev, on 19 Jan, 2010 - 09:20 AM, said:

what is the use of this keyword in java???

thanx
Vyom


thanx but i didn't get the answer of this question...i have read sun java's tutorial but iam not getting anything other than this that this keyword refers to current object...and i have tried some examples but i m not getting the practical use of this keyword.Plus this keyword is used in variable hiding....PLz can you give me any practical idea about whats the use of this keyword.

Thanx
Vyom
Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: JAVA Packages

Posted 20 January 2010 - 08:24 AM

Okay, say you have a class X with variables x, y, and z. Your class would look like this:

public class X {

private int x = 0; // these are made private so no other class can access them
private int y = 0; // they are also initialized to 0 so you don't have any nulls
private int z = 0;

public X() {

} // end constructor

} // end class



But, you want to accept variables to replace the 0's for x, y, and z so you can manipulate those for whatever calculations. So, your class would look more like this:

public class X {

private int x = 0;
private int y = 0;
private int z = 0;

public X(int x, int y, int z) {

this.x = x;
this.y = y;
this.z = z;

public int sum() {

int sum = x + y + z;
return sum;

} // end sum method

} // end constructor

} // end class



Okay, lets look at what's going on. First, I have those variables, just generic variables for this examble (in yours it will probably be more descriptive), and they're instantiated to 0 to avoid any null errors that may or may not occur. You have 1 constructor, which takes 3 interger types, and assigns them to the variables you declared earlier in the class. Because they are of the same name, it is helpful if you use the this keyword to denote you are referring to a variable from this class. That's really all it's for, is to denote that you are using the current class's variable. Basically saying "this class's variable x is being revalued to the value of the parameter x."

Now, as you may or may not have learned earlier, you always put the variable you are assigning before the equals sign (=) and what you are assigning to it after. Because you are using the same name, as I said before, this clarifies which variable is being altered. So, in essence, the variable X.x is being changed to the inputed number x from the parameters, X.y is being changed to the inputed number y from the parameters, and so on.

In the little sum method below, it is using the class's variables x, y, and z to alter the value of sum.

Hope this helped. I might favorite this, because I might use this in my next installment of Basic Java for N00blets: part 3 :D

This post has been edited by NeoTifa: 20 January 2010 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#13 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: JAVA Packages

Posted 20 January 2010 - 10:24 AM

View PostNeoTifa, on 20 Jan, 2010 - 07:24 AM, said:

Hope this helped. I might favorite this, because I might use this in my next installment of Basic Java for N00blets: part 3 :D



Thanx a ton man :^: very well explained :)
Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: JAVA Packages

Posted 20 January 2010 - 10:30 AM

I'm a chick! And you should thank me for it properly! :D
Was This Post Helpful? 1
  • +
  • -

#15 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: JAVA Packages

Posted 20 January 2010 - 11:07 AM

View PostNeoTifa, on 20 Jan, 2010 - 11:30 AM, said:

I'm a chick!


Rolf, maybe you should put that in your sig, to avoid any possible confusion :D
Was This Post Helpful? 0
  • +
  • -

#16 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: JAVA Packages

Posted 20 January 2010 - 12:12 PM

As if Hello Kitty and Princess Zelda didn't give it away already.... >_>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1