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!
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.include derived.java;part of your file? that is no valid java codenew Derived()is NOT the same asnew 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.