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!

user3085866
  • 125
  • 2
  • 5
  • 17
  • file name is same main1.java – user3085866 Feb 21 '14 at 18:55
  • [Start here](http://docs.oracle.com/javase/tutorial/java/concepts/class.html). 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. – turbo Feb 21 '14 at 18:55
  • is `include derived.java;` part of your file? that is no valid java code – exception1 Feb 21 '14 at 18:56
  • no,in c++ we inlcude class by writing #include classname – user3085866 Feb 21 '14 at 19:02
  • i am asking for a way to include or use that seperate derived class in my main – user3085866 Feb 21 '14 at 19:02
  • my project name is learning classes ,which has a default package which has main1.java and derived.java .now i want to import derived.java in main1.java and create an object of derived class from derived.java in main1 ? – user3085866 Feb 21 '14 at 19:06
  • wheni write import derived.java it gives an error saying"the import derived cannot be resolved – user3085866 Feb 21 '14 at 19:07
  • java is case sentitive derived is not equal to Derived – Kick Feb 21 '14 at 19:11
  • my derived class is in lowercase like derived.java 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. wheni do that it gives an error saying import derived cannot be resolved – user3085866 Feb 21 '14 at 19:14
  • Don't add the .java, just `import derived;`. FYI, class names should always start with a capital letter. It's not even necessary to import derived if it is in the same package. – turbo Feb 21 '14 at 19:19
  • it still gives an error saying 'import dervied canno be resolved' – user3085866 Feb 21 '14 at 19:21
  • it is not recognizing derived when i am trying to create an object of the derived class Derived derivedThread = new Derived(); – user3085866 Feb 21 '14 at 19:22
  • 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. – turbo Feb 21 '14 at 19:22
  • i guess i am way too tired , now i get you ,thanks its done <3 – user3085866 Feb 21 '14 at 19:24
  • 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. – turbo Feb 21 '14 at 19:30

2 Answers2

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.

Community
  • 1
  • 1
turbo
  • 1,887
  • 2
  • 21
  • 35
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.

Kick
  • 4,823
  • 3
  • 22
  • 29
  • 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? – user3085866 Feb 21 '14 at 19:03
  • 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. – user3085866 Feb 21 '14 at 19:12
  • wheni do that it gives an error saying import derived cannot be resolved – user3085866 Feb 21 '14 at 19:13
  • dear you have created derived class and you are trying to import Derived class.Java is case sensitive language.Check my answer – Kick Feb 21 '14 at 19:14
  • dear i am not trying to import Derived.java.instead i am writing import derived.java – user3085866 Feb 21 '14 at 19:17
  • If both the class exist in same package then no need to import any class.Also proper way to import any class is import derived; only – Kick Feb 21 '14 at 19:19