i am bit new in java what i want to achieve is, I have 2 java classes in same package.
CodeAnalyzer.java and CodeReader.java
CodeReader is the Jform class in which i have made JFileChooser, now i want to execute CodeReader with the execution of CodeAnalyzer.
Source code of CodeAnalyzer is Below
public class CodeAnalyzer {
public static void main(String[] args) {
// TODO code application logic here
}
}
public class CodeAnalyzer {
CodeReader cr;
public static void main(String[] args) {
cr = new CodeReader();
}
}
Just make an Object of your CodeReader and if it executes a Code which promts a Window it'll do it
Related
This may be a bit of a silly question, but I'm newish to Lamda Expressions and programming in General.
After experimenting around with Lambda Expression like so :
interface Starter {
public void start();
}
class Machine {
public void run( Starter s ) {
System.out.println("Running code....");
s.start();
}
}
public static void main(String[] args){
Machine mac1 = new Machine();
mac1.run(() -> System.out.println("Hello World"));
}
It made me wonder, if the Interface, and the class (Machine in this case) can be moved into seperate files for this to still work? I tried doing it how I think it would work, but that didn't work, is this possible? and if so how do you do it?
As long as the Machine class and Starter interface are visible to your main class, then this is perfectly possible.
My guess is that you moved the Starter interface to another package than the main class, and as you have not included the "public" modifier, it is no longer visible to the main class and will refuse to compile.
If I understood your problem correct, than just add a separate class (say App.class), that contains public static void main method:
public interface Starter {
public void start();
}
public class Machine {
public void run( Starter s ) {
System.out.println("Running code....");
s.start();
}
}
public class App {
public static void main(String[] args){
Machine mac1 = new Machine();
mac1.run(() -> System.out.println("Hello World"));
}
}
I have a static nested class StatClass, the file name is Example.java
class OuterClass
{
public static class StatClass
{
System.out.println("Hello"); // This line doesn't work
void pow()
{ System.out.println("Hello W");}
}
}
public class Example
{
public static void main(String[] args)
{
OuterClass.StatClass statInner1 = new OuterClass.StatClass();
statInner1.pow();
}
}
The first println statement doesn't work in the static nested class i.e if its removed then the program compiles, although the println statement works when inside the pow() method, couldn't understand this
Like in a normal class, you can't just put statements inside a class. You could put it in an initializer-block like this:
{
System.out.println("Hello");
}
Or in a constructor, or a method. Whatever you want.
I am just uniting the two answers so far:
one option is a static block
class OuterClass
{
public static class StatClass
{
static {
System.out.println("Hello"); // This line doesn't work
}
void pow()
{ System.out.println("Hello W");}
}
}
public class Example
{
public static void main(String[] args)
{
OuterClass.StatClass statInner1 = new OuterClass.StatClass();
statInner1.pow();
OuterClass.StatClass statInner2 = new OuterClass.StatClass();
statInner2.pow();
}
}
The main difference is how the block is executed by the JVM. In the first case it is executed once during the class initialization. Then the output will be :
Hello
Hello W
Hello W
Another option is initializer block in which case the code will be executed on each instantiation of the class.
class OuterClass
{
public static class StatClass
{
{
System.out.println("Hello"); // This line doesn't work
}
void pow()
{ System.out.println("Hello W");}
}
}
public class Example
{
public static void main(String[] args)
{
OuterClass.StatClass statInner1 = new OuterClass.StatClass();
statInner1.pow();
OuterClass.StatClass statInner2 = new OuterClass.StatClass();
statInner2.pow();
}
}
Then the output will be:
Hello Hello W Hello Hello W
Following statement is outside method that wont work
System.out.println("Hello"); // This line doesn't work
You have to wrap these statements inside static block as follows
static{
System.out.println("Hello"); // This line will work
}
this block will execute when JVM create static instances.
System.out.println("Hello");
This does not work because,the control never goes to the line. Unless you define this inside a function and use the object you created to call it OR use the static block which does not require an object to call it OR an initializer block, it will not work. I hope that helps
Eclipse is refusing to compile my java files when there are more than one main methods in the same package. As far as I know, this should work, but it compiles once and then never updates the .class files in the bin folder. Is there any way to force Eclipse to compile the class files?
Java allows only one method as main method which have the following syntax.
public static void main(String[] args) {
}
or
static public void main(String[] args) {
}
If you will try to make the duplicate main method then it won't allow because the JVM becomes confused to make decision while calling the main method.
But here you can try the methods like.
public static void main(int[] args) {
}
or
public void main(String[] args) {
}
or any methods which doesn't have the exact method as
public static void main(String[] args) {
}
This program must print final parametres about a TV state in three (3) rows...
in first row...on which level loudness is...
in the second on which program is (1., 2., 56, etc...)
third row - is it turned on...
(after all operations inside programm)... this programm is pasted from manual for Java...
class Television {
int volumeTone = 0;
int channelNow = 1;
boolean turnedOn = false;
void turnOn(){
turnedOn = true;
}
void turnOff(){
turnedOn = false;
}
void increaseVolume(){
volumeTone = volumeTone + 1;
}
void decreaseVolume(){
volumeTone = volumeTone - 1;
}
void turnOffVolume(){
volumeTone = 0;
}
void changeChannelUp(){
channelNow = channelNow + 1;
}
void changeChannelDown(){
channelNow = channelNow - 1;
}
int returnChannelBefore(){
return channelNow;
}
int returnToneVolume(){
return volumeTone;
}
boolean isItTurnedOn(){
return turnedOn;
}
void writeParametres(){
System.out.println("Volume loudness now is "+volumeTone);
System.out.println("Channel now is "+channelNow);
System.out.println("Tv is turned on? "+turnedOn);
}
}
You need to have a main method in the class to run...
Your exception itself say that, please define the main method as:public static void main(String[] args)
You need main method in your class Television as shown below.
public static void main(String[] args)
{
//Your logic run this class
}
In Java, you need to have a method named main in at least one class.
Please refer this link about main method.
Without a main method, you will not be able to run anything. Create a new class with a main method like so:
public class myClass
{
public static void main(String [] args)
{
//Here you can create an instance of your television class
Television tel = new Television();
//You can then call methods on your newly created instance
tel.turnOn();
}
}
The main entrance to a java application is with a static main method
which is usually defined as;
public static void main(String []args)
{
}
If you want to run any class make sure it has this method.
You will need to Implement main method
From The Java Tutorials' Hello World Example: (emphasis mine)
In the Java programming language, every application must contain a main method whose signature is: public static void main(String[] args)
...
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
In other words, when running a Java class, the JVM will look for a main() method in it. That method will be called automatically when running the class, and will be the entry point for the execution flow of the program.
Also, take a look at How does the main method work?.
I was trying to know what would happen if I have two classes with the main function.
I used the following code:
class A {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
class Hello {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
I compiled it using javac First.java (since no class is specified as public , I named the file as First.java); it got compiled without any error and i ran only the class A.Expecting the Hello class to run itself. DIDN'T HAPPEN(?),maybe the program ran out of scope.
So,
I tried compiling the following java code(I am a beginner) but i got the following error.
Code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello,World!");
}
}
class A {
public static void main(String[] args) {
System.out.println("Hello,World!");
Hello.main();
}
}
I compiled it through javac First.javaand got the following error:
method main in class Hello cannot be applied to given types;
Hello.main();
^
I wanted the program to run first the class A's main function and then class Hello's.
What's going wrong here?
Look at the declaration of Hello.main:
public static void main(String[] args)
Now you're trying to call it like this:
Hello.main();
What would you expect the value of args to be, within the method? You need to provide it with a value for args... and fortunately, you already have one, as you're within a method which uses args as a parameter, also of type String[]. So you should just be able to change your code to:
Hello.main(args);
Note that the two args parameters - one for Hello.main and one for A.main are entirely separate. We happen to use pass the value of one to the provide the initial value for the other, but we could easily have written:
Hello.main(null);
or
Hello.main(new String[] { "Some", "other", "strings" });
instead.
Do this in your second class:
public static void main(String[] args) throws IOException {
MyOtherClass.main(args);
}