[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem in execution

Posted on 1998-11-23
2
Medium Priority
?
185 Views
Last Modified: 2010-03-30
Hello,

  Consider the following code.

import javlib.in.*;

class testforss1
{
            testforss1()
            {
                  prt.disp("ur in the base class");
            }
}
      
class testforss
{
            private testforss1[] t1 = new testforss1[10];
            private int x;

            testforss()
            {
                  System.out.println("Its okay");
            }
            Random rand = new Random();
            int x1 = rand.nextInt();
            
            System.out.println("The output is " + (int)x1);
            public static void main(String[] args)
            {
                        testforss[] t = new testforss[1];
                        System.out.println("The length is " + t.length);
            }
}

I am just trying to print the variable x1 when i compile this code it is saying the following error

C:\SSLIB\testforss.java:23: Type expected.
            System.out.println("The output is " + (int)x1);
                               ^
1 error
Finished

What might be the error??

regards
Nature
0
Comment
Question by:nature
2 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 60 total points
ID: 1228036

in the object definition you can have
-) field definitions (that is  Random rand = new Random(); )
-) method definitions (that is public void main(String[] args) {})
but not random pieces of code.

the error that you receive

>> C:\SSLIB\testforss.java:23: Type expected.
>> System.out.println("The output is " + (int)x1);

means that comiler is expecting another field or method definition, not a code ...

so - you must put this code in your constructor or some other method ...

hope this helps
  heyhey

0
 

Author Comment

by:nature
ID: 1228037
Yep it works thanks

nature
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question