0

I am a beginner at java, I used to code in C++ and there when using classes I used to define them in separate files and then include those classes in my main file.

I'm trying to learn threads for socket programming so I can open multiple server ports as threads and accept multiple clients. I know that in Java the file name should be the same as the class name (correct me if i am wrong). This is what I am trying to do:

main.java

include derived.java; class main1 { main1() { System.out.println("Constructor of main1 class."); } void main1_method() { System.out.println("method of main 1 class"); } public static void main(String[] args) { main1 my = new main1(); Derived derivedThread = new Derived(); derivedThread.start(); } } 

derived.java

public class derived extends Thread { public void run() { System.out.println("starting a new thread"); } } 

How can I create a derived class object in main and include it in my main1.java file?

I think I do not fully understand how classes work in Java and what classpath has to be used with it. I have a deadline for my networking project and I am very behind so please help me!

15
  • file name is same main1.java Commented Feb 21, 2014 at 18:55
  • Start here. In java you import by import name.of.package.className. Overall, this is not a good example of Java classes, and short of writing them for you, it would be difficult to tell you how to correct them. Please see the tutorial I linked. Commented Feb 21, 2014 at 18:55
  • is include derived.java; part of your file? that is no valid java code Commented Feb 21, 2014 at 18:56
  • 1
    You don't need to import it, you said yourself that both classes are in the same package. It's not recognizing it because new Derived() is NOT the same as new derived(). Capitalization matters in Java, and your class name is lower case, while you're trying to make an object with upper case. Derived should be capitalized in the file name, class name, constructor, and ANY calls to the constructor. Commented Feb 21, 2014 at 19:22
  • 1
    See my answer for how this would look the "java" way. I think your issue is mostly confusing C++ and Java syntax. You're close, but just keep working at it and you'll be fine. Commented Feb 21, 2014 at 19:30

2 Answers 2

1

Delete your files and try this, this is how it should look in Java:

Derived class: Derived.java

public class Derived extends Thread { public void run() { System.out.println("starting a new thread"); } } 

Main1 class: Main1.java

public class Main1 { public Main1() { System.out.println("Constructor of main1 class."); } void main1_method() { System.out.println("method of main 1 class"); } public static void main(String[] args) { Main1 my = new Main1(); Derived derivedThread = new Derived(); derivedThread.start(); } } 

Note:

1) Class names are always capitalized, and you are right, the filename must be the same. In addition, the constructor and any calls to the constructor must be capitalized.

2) If you put classes in the same package, you don't need to import them. If you have multiple packages, you would import like so: import packageName.className;. No need for .java at the end, strictly the class name. You can also have nested packages, so you might see things like: import java.util.ArrayList;. This would be using ArrayList class, found in the util package, which is in the java package (built in). You shouldn't have to worry much about making nested packages on smaller projects, but that's the concept.

3) Notice I added the public modifier to Main1 and it's constructor. It is good practice to give a modifier to class names and methods, as well as class variables. See this SO Question for information about modifiers. For a beginner, you should mostly only be concerned with public and private.

I hope that helps, and good luck with your Java studies.

Sign up to request clarification or add additional context in comments.

Comments

0

No need to use this include derived.java; .Use import if the derived class exist in different package.The class Derived is different while calling and declaring.

class main1 // Class name must start with Uppercase { public static void main(String[] args) { main1 my = new main1(); // Can be remove Derived derivedThread = new Derived(); derivedThread.start(); } } public class derived extends Thread // Change derived to Derived --------------------^ { public void run() { System.out.println("starting a new thread"); } } 

I have remove constructor and one method which is not used.

6 Comments

i want to learn how to use a seperately written class in a .java file in my main file to create its objects,you get my point?
i have the project which has a default package.default package has main1.java and derived.java. i want to import the derived.java in main1.java.how do i do that.
wheni do that it gives an error saying import derived cannot be resolved
dear you have created derived class and you are trying to import Derived class.Java is case sensitive language.Check my answer
dear i am not trying to import Derived.java.instead i am writing import derived.java
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.