Why is it saying that my public class is not defined when I only have one public class and the class file - drjava

import java.util.Scanner;
public class Lab6_2{
public static void main(String[] args){
Scanner sc_1 = new Scanner(System.in);
int numnber, i;
System.out.println("Enter an integer between 1 and 10.");
number = sc_1.nextInt();
}
}
This code is saying "2 errors found:
File: C:\Users\danwr\Downloads\lab 6_2.java [line: 2]
Error: The public type Lab6_2 must be defined in its own file
File: C:\Users\danwr\Downloads\lab 6_2.java [line: 7]
Error: number cannot be resolved to a variable"

Based on your error message I see two issues. The first is it appears that your .java file is named "lab 6_2.java" (with a space character). It needs to be named "lab6_2.java". However, be careful with this - Windows doesn't care that you use "lab" vs. "Lab" but on Unix the file would have to be named "Lab6_2.java".
As for the second error - where is number defined? (and is it spelled correctly)?

Related

bytebuddy official demo throw Exception "Class already loaded: class foo.Bar"

Section "Working with unloaded classes" in Official document give a demo, I run it on my machine then throw an exception Class already loaded: class foo.Bar。
class MyApplication {
public static void main(String[] args) {
TypePool typePool = TypePool.Default.ofSystemLoader();
new ByteBuddy()
.redefine(typePool.describe("foo.Bar").resolve(), // do not use 'Bar.class'
ClassFileLocator.ForClassLoader.ofSystemLoader())
.defineField("qux", String.class) // we learn more about defining fields later
.make()
.load(ClassLoader.getSystemClassLoader());
assertThat(Bar.class.getDeclaredField("qux"), notNullValue());
}
}
bytebuddy version is 1.10.22
The problem is in the last line Bar.class.getDeclaredField("qux") which loads the Bar class upon validation of the code. I fixed this in the example. Rather use the return value of load which returns Bar.

Can't update a class method with javassist

I'm playing with javassist (to use it later on a project) but I don't manage to make a simple update to a class.
I try to insert code before a method but its not being executed.
I've a gradle project and I'm using javassist version: '3.27.0-GA'.
Given the following class:
public class Dummy{
public int dummy(){
return 5;
}
}
The following test fails, so the class is not being modified:
#Test
public void modifyReturnValueTest() throws NotFoundException, CannotCompileException, IOException {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("Dummy");
CtMethod m = cc.getDeclaredMethod("dummy");
m.insertBefore("{ if(true) return 3; }");
cc.writeFile();
assertEquals(3, new Dummy().dummy());
}
I'm missing something?
My guess is that when you call new Dummy().dummy() in
assertEquals(3, new Dummy().dummy());
the class loader loads the original version of Dummy class.
Since you want to load/use the version that you modified represented by the CtClass instance cc, you can insert the following snippet before assertEquals to make the class loader load the modified version of the Dummy class instead:
cc.toClass();
then the assert should be successful.
Note that in order to use toClass() above we rely on the fact that the Dummy class is never loaded before the toClass() invocation. (It will throw exception otherwise, the class loader cannot load two different versions of the same class at the same time)
You can check the javassist documentation for more details. This section might be especially useful http://www.javassist.org/tutorial/tutorial.html#load.

Static Object of a class in another class in aggregation relationship C++

I have two classes first class contains the object of another class as static but C++ doesnot allow me to do this and gives me some error.
source.cpp
#include"control.h"
int main()
{
Controller cnt;
cnt.tempcont();
return 0;
}
control.h
#include"recorder.h"
class Controller
{
public:
static recorder rec;
void tempcont();
};
recorder Controller::rec;
control.cpp
#include"control.h"
void Controller::tempcont()
{
rec.temprec();
}
recorder.h
#include<iostream>
using namespace std;
class recorder
{
public:
int a;
void temprec();
};
recorder.cpp
#include"recorder.h"
void recorder::temprec()
{
cout << "temp rec called";
}
I am getting the following errors and i have no idea why these errors are comming..
Error LNK1169 one or more multiply defined symbols found
Error LNK2005 "public: static class recorder Controller::rec" (?rec#Controller##2Vrecorder##A) already defined in control.obj
You define the variable Controller::rec in the header file. That means the variable will be defined in every translation unit where that header file have been included. It should only be defined in one single translation unit.
This is very easy to do: Just move the definition to a single source file.

unable to chain up to base constructor requiring arguments

public class Font : SDLTTF.Font {
public Font (string _filename, int _size) {
}
public void draw () {
}
}
That's my code. When I try to build it, I get:
Font.vala:4.5-4.15: error: unable to chain up to base constructor requiring arguments
public Font (string _filename, int _size) {
^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
I thought I needed to override the constructor, so I tried to public override it, but now I get:
Font.vala:4.5-4.24: error: abstract, virtual, and override modifiers are not applicable to creation methods
public override Font (string _filename, int _size) {
^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
Any ideas on how to fix this? I'm trying to inherit the SDLTTF.Font class.
Have you tried putting
base(_filename, _size);
in your constructor?
EDIT:
This worked for me. Note however that SDLTTF.Font is defined in the vapi as a compact class, meaning that when you derive it, you're only allowed to define new functions for your subclass, but no instance data (member variables, etc.). If you require this, I'd recommend you go with apmasell's suggestion and create a wrapper class deriving from (G)Object.
SDLTTF is not managed by GObject, so Vala cannot create a derived class. Vala can only create derived classes if they make use of GObject, as is typical in GLib, GTK+, Pango, ATK, and many GNOME libraries.
Depending on what you want to do, you could create a new class that contains an instance of SDLTFF.Font and proxy the appropriate requests.

Problems about java syntax [duplicate]

This question already has answers here:
Can a java file have more than one class?
(18 answers)
Closed 8 years ago.
Here's the code :
public class EmployeeTest
{
public static void main(String args[]){
//System.out.println("hello world");
Employee aEmployee = new Employee("David",1000);
System.out.println(aEmployee.getName() + aEmployee.getSalary());
}
}
class Employee // **why can't I put a "public" here**
{
// Constructor
public Employee(String name, double salary)
{
this.name = name;
this.salary = salary;
}
// Methods
public String getName()
{
return this.name;
}
public double getSalary()
{
return this.salary;
}
// instance field
private String name;
private double salary;
}
My question is : in the second class definition's first line, why can't I put a "public" to define it ?
What's the exactly meaning of "public" when using it defines a class ?
This is language feature. There must be only one top-level public class per .java file and public class name must match the source java file name.
Basically, non-public types are not accessible outside the package so if you wish to allow type to be used anywhere then make it public.
Never create a type in default package. (Always use package)
Employee.java
package com.abc.model;
public class Employee{..}
EmployeeTest.java
package com.abc.test;
public class EmployeeTest{ ... }
Because a Java source file can have at most one top-level public class or interface, and the name of the source file must be the same as the name of that class or interface.
That's a rule that the Java compiler of Oracle's JDK imposes.
In Java, there can only be a single public top level class per source file and it needs to be named the same as the file.
This is useful for the compiler when it needs to locate a class definition from outside the package, since it knows the type name, it knows which class file to find the class in. For example. since a jar file is in essence a zip file with class files, this prevents the compiler from having to unzip the entire jar to find a class definition.
The Java language specification §7.6 specifies this as an optional restriction;
If and only if packages are stored in a file system (§7.2), the host
system may choose to enforce the restriction that it is a compile-time
error if a type is not found in a file under a name composed of the
type name plus an extension (such as .java or .jav) if either of the
following is true:
The type is referred to by code in other compilation units of the
package in which the type is declared.
The type is declared public (and therefore is potentially accessible
from code in other packages).
you can define a public class inside a public class which is legal.
public class EmployeeTest
{
public class Employee {
}
}