13 Replies - 733 Views - Last Post: 20 April 2009 - 07:24 PM Rate Topic: -----

#1 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Java Beginner needing help

Post icon  Posted 20 April 2009 - 01:35 PM

So this isn't really a question involving code needing fixed. I was messing around with classes/object oriented programming in Java. And I noticed one of the errors saying that "Record class public, should be declared in file name Record.java". How exactly do you do that? Just cut+paste it into a .java file saved into the same folder? But then I imagine you have to have some kind of include statement in the beginning.

Second question: What code goes in the beginning? In C++ you have your include statements and "using std namespace" or w/e. I imagine something similar to that occurs in Java right?

Is This A Good Question/Topic? 0
  • +

Replies To: Java Beginner needing help

#2 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: Java Beginner needing help

Posted 20 April 2009 - 01:46 PM

In Java they have import statements, and when you declare a new class, it's better to make a seperate file and include it in your package that you're working on.
Was This Post Helpful? 1
  • +
  • -

#3 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Java Beginner needing help

Posted 20 April 2009 - 02:00 PM

View PostNeoTifa, on 20 Apr, 2009 - 12:46 PM, said:

In Java they have import statements, and when you declare a new class, it's better to make a seperate file and include it in your package that you're working on.


So instead of #include "Record.cpp" you do import "Record.java"?

And does the Record.java file have to have some kind of #ifindef statement like in C?
Was This Post Helpful? 0
  • +
  • -

#4 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Java Beginner needing help

Posted 20 April 2009 - 02:13 PM

no ""s
just like
import java.util.*;

also make sure you compiled it and the external class is in the same folder

This post has been edited by sl4ck3r: 20 April 2009 - 02:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Java Beginner needing help

Posted 20 April 2009 - 02:35 PM

View Postsl4ck3r, on 20 Apr, 2009 - 01:13 PM, said:

no ""s
just like
import java.util.*;

also make sure you compiled it and the external class is in the same folder


Wait I'm confused....

Isn't a class supposed to be named Record.java (assuming the class is named Record).

So shouldn't it be:

import Record.java
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: Java Beginner needing help

Posted 20 April 2009 - 02:36 PM

I do not know what IDE you are using, but when you open a project folder, it automatically has one class file. In the file menu, there is generally a "New Class" button, and you can input the name, and the IDE will automatically put that class in your source folder, and will automatically be put into your package.
Was This Post Helpful? 0
  • +
  • -

#7 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Java Beginner needing help

Posted 20 April 2009 - 02:48 PM

View PostNeoTifa, on 20 Apr, 2009 - 01:36 PM, said:

I do not know what IDE you are using, but when you open a project folder, it automatically has one class file. In the file menu, there is generally a "New Class" button, and you can input the name, and the IDE will automatically put that class in your source folder, and will automatically be put into your package.


NetBeans is the IDE I use.

I see where I could make a new class in the source packages folder by right-clicking on it. Are you saying it automatically puts the "import" statement at the beginning of your program too?
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: Java Beginner needing help

Posted 20 April 2009 - 02:52 PM

You don't need one unless you're importing other project packages into the current one. It should say

package ilubdic;



That's basically it's import statement, saying it's part of that "package".
Was This Post Helpful? 0
  • +
  • -

#9 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Java Beginner needing help

Posted 20 April 2009 - 04:25 PM

View PostNeoTifa, on 20 Apr, 2009 - 01:52 PM, said:

You don't need one unless you're importing other project packages into the current one. It should say

package ilubdic;



That's basically it's import statement, saying it's part of that "package".


I'm confused still...

Lets say I have 2 classes, Record and PhoneBook.java Saved into Record.java and PhoneBook.java, and they are saved in the 'Project1' external folder with the main source code (i suppose that means just the main class/function).

What code do you put in the main source file to include both classes?

Is "ilubdic" a class or an external folder or something else?

This post has been edited by agentkirb: 20 April 2009 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: Java Beginner needing help

Posted 20 April 2009 - 05:32 PM

No it was an example. You don't put anything in the first class file. They're already linked. You have to instantiate the method to use it, but the compiler knows they're linked.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Beginner needing help

Posted 20 April 2009 - 05:45 PM

OK lets talk C++

In C++ you have .h and .cpp
Java does not have .h (you will understand later why they are useless)

If the files are in the same directory... no import at all are required they are all in the same "workspace"
(so if Java was C++ it would automatically #include *.h from the current directory)
Was This Post Helpful? 0
  • +
  • -

#12 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Java Beginner needing help

Posted 20 April 2009 - 06:09 PM

View Postpbl, on 20 Apr, 2009 - 04:45 PM, said:

OK lets talk C++

In C++ you have .h and .cpp
Java does not have .h (you will understand later why they are useless)

If the files are in the same directory... no import at all are required they are all in the same "workspace"
(so if Java was C++ it would automatically #include *.h from the current directory)


So basically the first line of code should be the main class if everything is in the same folder. That answers my question.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Beginner needing help

Posted 20 April 2009 - 06:19 PM

View Postagentkirb, on 20 Apr, 2009 - 05:09 PM, said:

So basically the first line of code should be the main class if everything is in the same folder. That answers my question.

Great :^:
Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: Java Beginner needing help

Posted 20 April 2009 - 07:24 PM

>_> I hate you pbl...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1