I cant understand whats wrong with my code. In this code i getting errors like this when im trying to assign a value to a variable within a class. Also System.out.println doesnt work in this classes:
1.Identifier expected
2.Unexpected token
3.Unknown class "windows"
public static void main(String[] args) {
}
class building{
int apart_num;
apart_num = 3;
}
class apartments{
double area;
int lightbulb;
int windows;
windows = 4;
}
interface construct_building{
}
interface construct_apartments{
}
You are declaring an instance member on one line :
int apart_num;
and then assign a value to it :
apart_num = 3;
The problem is that this is done outside a method, those statement can't be separated, you can't assign a variable previously declare outside a block statement so.
Do it in a one line (declaration and assignation) :
class building{
int apart_num = 3;
}
or with a constructor
class building{
int apart_num;
public building(){
apart_num = 3;
}
}
or in a block statement
int windows;
{ //a block statement
windows = 4;
}
Then, if this code is not a class, you need to do it.
public MyClass{
public static void main(String[] args){ ... }
...
class Building { //inner class (will exist only inside of a MyClass instance
}
static class Apartment { // a nested class, exist whitout a MyClass instance
}
}
class Level { //A class that have nothing to do with MyClass and that can not be public.
}
Where MyClass is the name of the file (MyClass.java)
Related
For below piece of code,
class Test {
int i;
}
public class Testable {
public static void main(){
new Test().i;
}
}
I am getting 'error: not a statement' for line new Test().i;
new Test().i is indeed not a statement. You need to do something with this expression, like, e.g., saving it to a variable:
int i = new Test().i;
I want to create a wrapper class that calls static methods and member fields from a class that is provided by a library I am unable to view the code.
This is to avoid boilerplate setting code of the global member fields when I need to use a static method in a specific context.
I want to try to avoid creating wrapper methods for each static method.
My question:
Is it possible to return a class with static methods from a method to access just the static methods without instantiating it?
Code is below with comments in-line.
The code is used to demonstrate a change in a static value when the method getMath() is invoked.
I want to avoid the setting of the value before calling the static method.
StaticMath.setFirstNumber(1);
StaticMath.calc(1);
StaticMath.setFirstNumber(2);
StaticMath.calc(1);
I am using the Eclipse IDE and it comes up with Warnings, which I understand, but want to avoid.
I tried searching for something on this subject, so if anyone can provide a link I can close this.
public class Demo {
// Static Methods in a class library I don't have access to.
static class StaticMath {
private static int firstNum;
private StaticMath() {
}
public static int calc(int secondNum) {
return firstNum + secondNum;
}
public static void setFirstNumber(int firstNum) {
StaticMath.firstNum = firstNum;
}
}
// Concrete Class
static class MathBook {
private int firstNum;
public MathBook(int firstNum) {
this.firstNum = firstNum;
}
// Non-static method that gets the class with the static methods.
public StaticMath getMath() {
StaticMath.setFirstNumber(firstNum);
// I don't want to instantiate the class.
return new StaticMath();
}
}
public static void main(String... args) {
MathBook m1 = new MathBook(1);
MathBook m2 = new MathBook(2);
// I want to avoid the "static-access" warning.
// Answer is 2
System.out.println(String.valueOf(m1.getMath().calc(1)));
// Answer is 3
System.out.println(String.valueOf(m2.getMath().calc(1)));
}
}
I'd just wrap it to make for an atomic operation:
public static class MyMath{
public static synchronized int myCalc( int num1 , int num2 ){
StaticMath.setFirstNum(num1);
return StaticMath.calc(num2);
}
}
Drawback: You'll have to make sure, StaticMath is not used avoiding this "bridging" class.
Usage:
int result1 = MyMath.myCalc( 1, 1 );
int result1 = MyMath.myCalc( 2, 1 );
You shouldnt call a static method through an object reference. You should directly use class reference to call a static method like this:
StaticMath.calc(1)
But if you still need it for some reason, you can return null in getMath method, but you will still get warning in Eclipse:
public StaticMath getMath() {
StaticMath.setFirstNumber(firstNum);
return null;
}
I infer that question is not properly asked if the answer is not
StaticMath.calc(1)
Other issue you may be facing due to package visibility to static inner classes. Which is a design choice by the writer of Demo class. If you can mark your classes MathBook and StaticMath public then you can access them like below:
Demo.StaticMath.calc(1);
My main java code is like that:
package Javathesis;
//import... etc
//...
public class Javathesis; // My main class
{
public static void // There are a lot of these classes here
//...
//...
class x
{
String a;
String b;
x(String a, String b)
{
this.a = a;
this.b = b;
}
}
public void getAllDataDB1
{
ArrayList<ArrayList<x>> cells = new ArrayList<>();
while(resTablesData1.next())
{
ArrayList<CellValue> row = new ArrayList<>();
for (int k=0; k<colCount ; k++) {
String colName = rsmd.getColumnName(i);
Object o = resTablesData1.getObject(colName);
row.add(new x(rsmd.getColumnType(),o.toString());
}
cells.add(row);
}
public static void main(String[] args)
{
connectToDB1();
// i want to call an instance of class "x" HERE!!!
}
}
How can i call class x in public static void main? Am i doing something wrong? I already test the way i know
Class class = new Class();
class.x(String a, String b);
but i receive errors. Can somebody help me to fix this?
Thanks in advance.
Since x is an inner class it need an outer class instance to obtain a reference:
Class x = new Javathesis().new x(a, b);
In addition the semi-colon needs to be removed from the class declaration
Methods in x can be invoked using the reference created
Java Naming conventions show that classes start with an uppercase letter. Also its good to give the class a more meaningful name than a single letter name
There are multiple problems with this code:
Class class = new Class();
class.x(String a, String b);
You cannot name a variable 'class', it is a reserved word. Also, x is a class - you cannot just call it, you need to instantiate it.
Also, why would you instantiate a Class - which is a class encapsulating knowledge about Java class?
Something like that might work:
Javathesis thesis = new Javathesis();
Javathesis.x thesis_x = new thesis.x("a","b);
Also, please, start class names with the capital letter - it is a convention in Java.
You should create an instance of your inner class
YourInnerClass inner = new YourOuterClass().new YourInnerClass();
and then call a method on it
inner.doMyStuff();
To call a class method without instantiating an object from that class, the method must be declared as static, and then invoked from wherever you want using the class name dot method parentheses arguments.
public class x {
//constructor, etc
public static int add(int x,y) {
return x+y;
}
}
//make the call from somewhere else
int y = x.add(4,5);
class Myclass{
int x;
Myclass(int i){
x = i;
}
}
class UseMyclass { //Why do I need another class?
public static void main (String args[]){
Myclass y = new Myclass(10);
System.out.println(y.x);
}
}
Why can't I run main() out of Myclass?
From the book it says I would be running the UseMyclass so I guess that would be my file name. Why couldn't I just use the Myclass though as the file name and run main() in there?
I'm new to programming so I'm just trying to figure stuff out.
You don't actually need another class. If you just put the main method into the class, it will work. For example, this code will work just fine:
class Myclass{
int x;
Myclass(int i){
x = i;
}
public static void main (String args[]){
Myclass y = new Myclass(10);
System.out.println(y.x);
}
}
However, separating the main class is a good idea when you are dealing with large programs with many classes. Then, you can sneak unit tests into main methods in other classes.
This question already has answers here:
Is this a valid way to count instances of objects?
(2 answers)
Closed 9 years ago.
class Clasa {...}
class Test {
public static void main(String[] args){
Clasa x = new Clasa();
System.out.println(x.getNo());//displays 1
Clasa[] y = new Clasa[10];
for(int i = 0; i<4; i++)
y[i]=new Clasa();
System.out.println(y[0].getNo()); //displays 5
}
}
How can I replace those three dots, so GetNo() method call to return the number of instantiated objects of class Clasa. Test class should not be changed.
Add a static variable which acts as a counter and increment it inside the constructor and getNo returns the value of the static variable.
Static variable have their values kept across all instances of a class
class Clasa {
private static int nbInstances = 0;
public Clasa () {
nbInstances++;
}
public int getNo() {
return nbInstances;
}
}
I agree with Brian , on that the above code is not considering the GC. So I would like to replace your code with the below code snippet
package com.instance.main;
import com.instance.target.Clasa;
public class Test{
public static void main(String[] args) {
Clasa targetClass;
Object[] object=new Object[10];
for(int i=0;i<object.length;i++){
object[i]=new Clasa();
}
System.out.println("Number of Instantiate Object {Before Calling GC}: "+Clasa .getNumberOfInstatiateObj());
/* Here I am trying to deallocate the memory of Object at index no 9, so that GC called this unused object to deallocate it from memory*/
for(int i=0;i<object.length;i++){
if(i==8){
object[i]=object[i+1];
object[i+1]=null;
System.runFinalization();
System.gc();
}
}
}
}
just put the above code beneath the main method and also you have to modify your Clasa code from the below code
package com.instance.target;
class Clasa {
private static int nbInstances = 0;
public Clasa () {
nbInstances++;
}
public int getNo() {
return nbInstances;
}
public void finalize(){
nbInstances --;
System.out.println("Number of Instantiate Object {After Calling GC}: "+nbInstances );
}
}
After modifying your code by following the above steps , your code will give you your desired output.
Please let me correct if I am wrong some where.
Hi Dany I modified my code , so according to the above code ,you have to create your class under different package which written in the class code . And let me know if you are getting problem still.