0

I saw one of the link Can we write a program without a class in core Java?

So going through this , it looks like we cant have a java program without atleast one single class.

But we have some requirement like,

I have a test Java program like::

package dashboardName;
import org.testng.Assert;
import org.testng.annotations.Test;
import pojoclass.MpsPojo;
import mpsLogicPack.MpsLogic;
public class TestLogic {

        MpsPojo mpspojon = new MpsPojo();
        MpsLogic mpslogicn = new MpsLogic();
        @Test
        public void firstTest() {

                        mpspojon.setMfr("m1");
                        mpspojon.setProd("p1");
                        mpspojon.setSchema("sch1");
                        mpslogicn.calculateAssert(mpspojon);
                        System.out.println("Printing from Final class");
                        }
}

The name of this package is dashboardName.

So if I will write a java program which has just the import statement of dashboardName. like:

FinalTest.java

 import dashboardName.TestLogic;

So if I will execute this, what should be the result. Currently this is showing no error but showing No test run.

This may be a silly question because I belong to Perl backround and switching to Java. So pls excuse me.

Thanks

Community
  • 1
  • 1
undefined
  • 3,464
  • 11
  • 48
  • 90

3 Answers3

1

import statements only exist at compile time; they tell the compiler how to expand unqualified classnames. So, for example, import dashboardName.TestLogic; just means that all occurrences of the classname TestLogic should be expanded to dashboardName.TestLogic. But if you don't have any occurrences of the classname TestLogic, then this has no effect.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • So, I came to conclusion that, if I have to write a useful Java program, then this should have atleast a class. Thanks a lot. – undefined Jan 12 '14 at 19:39
1

Assuming I understood your question correctly, then no. imports are basically just a list of lookups for which exact classes matches (or more formally, which Fully Qualified Names you are using) to the short name you're using in your actual application code.

On the other side, working without imports is entirely possible if you place all your classes in the same directory and only use system classes from java.lang and java.util.

Esko
  • 29,022
  • 11
  • 55
  • 82
1

You cannot run without a class. In order to execute a java program, you should either have a main() method, for which you need a class. Or you could use static initialization blocks of a class, for which, naturally, you need a class again.

Samurai Girl
  • 978
  • 2
  • 8
  • 13